View Full Version : checking if theres a history
hypedup.co.uk
11-14-2002, 07:00 PM
hi. i want a script that checks if there is a page in the history. if not (ie the page the script is in is the first page they have been to), it sends them to another page.
anyone know what i need to put? thanks, paul
redhead
11-14-2002, 07:32 PM
this should work...
<script>
if (history.length == 0) {
window.location = "otherpage.html";
};
</script>
beetle
11-14-2002, 07:37 PM
The history object exposes only 1 property: length and 3 methods: go(), back(), foward()
So, basically, with JS, you can only know how long the history is and navigate it, but you cannot determine what is in it or even where you are in it.
You could create your own history tracking script using cookies (I've seen one, don't remember where) but wouldn't be of much use to you if the user has disabled cookies.
Could you use the document.referrer (http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/referrer.asp) property for what you need?
x_goose_x
11-14-2002, 07:41 PM
http://www.codingforums.com/showthread.php?s=&threadid=5404&highlight=history
Not fully tested. But give it a try.
beetle
11-14-2002, 07:42 PM
That's the one I've seen! :D
redhead
11-14-2002, 08:14 PM
oh right... sorry. i thought you just wanted a script that took the user somewhere else if the one being veiwed was the first page...
hypedup.co.uk
11-16-2002, 01:58 PM
what i want is that there are some pages that you should only go to if you go to it from 1 or 2 other pages. i currently have:
<script language="javascript">
if (document.referrer.indexOf("page1.html") == -1 || document.referrer.indexOf("page2.html") == -1) {
window.location = "start.html"; }
</script>
but this doesnt seem to be working properly. it doesnt look right either. it seems to say "if you have come from page1 or page2, go to start" when it actually does the opposite! does anything in this script need changing to make it work properly?
also, redhead i tried what you wrote but that doesnt work for what i want! i think what i have above is the way to go, i just need to get it to work properly.
thanks
beetle
11-16-2002, 02:21 PM
Yor condition is a bit flawed. For example, if 'page1.html' is not found, then indexOf returns a -1 and the first half to your condition evaluates as true. Since you are using OR (||) the 2nd condition is never checked, because the first is true and satisifes the overall condition. In short, you need AND (&&)
<script language="javascript">
if (document.referrer.indexOf("page1.html") == -1 && document.referrer.indexOf("page2.html") == -1) {
window.location = "start.html"; }
</script>
Wait....what is start.html? Is that page for your 'ok' users, or for the 'not ok' users? The above is for the 'not ok' users. If for the 'ok' users, then this is what you need...
<script language="javascript">
if (document.referrer.indexOf("page1.html") != -1 || document.referrer.indexOf("page2.html") != -1) {
window.location = "start.html"; }
</script>
hypedup.co.uk
11-16-2002, 03:09 PM
thanks beetle, the start.html was for 'not ok' users. the only problem is this doesnt work if the person clicks the link whilst holding down shift and so opens up the page in a new window. if they do this, they should go to the start.html page as well. what do i need to do to change it to do this as well?
beetle
11-16-2002, 03:28 PM
<script language="javascript">
var r = document.referrer;
if ((r.indexOf("page1.html") == -1 && r.indexOf("page2.html") == -1) || history.length == 0) {
window.location = "start.html"; }
</script>
hypedup.co.uk
11-16-2002, 05:14 PM
thank you, that has worked. i was originally using:
if (document.referrer.indexOf("page1.html") == -1 || document.referrer.indexOf("page2.html") == -1 || history.length == 0) {
window.location = "start.html"; }
before i realised i needed to use &&. thanks beetle.
but i do have one more problem! the first page loads in a popup window from the main page. how do i get it so that page only loads if its inside the window and not as a seperate one? i did use "if (history.length != 0) go to start.html" which works the first time you go in it, but as you can go to the start page at a later time, i need to use something else. any ideas?
beetle
11-16-2002, 06:38 PM
Uhm, maybe
if (!window.opener)
??
hypedup.co.uk
11-16-2002, 06:52 PM
i do already have that, im using the following but it doesnt work:
<script language="javascript">
if (!window.opener) {
window.location = "start.html"; }
</script>
beetle
11-16-2002, 06:54 PM
What do you get when you...
alert(window.opener);
??
hypedup.co.uk
11-16-2002, 07:07 PM
if doing it through the popup it comes up [object] but if doing it directly from the page it comes up undefined.
so does that mean i could do:
if (window.opener == undefined) {
window.location = "cheat.html"; }
?
joh6nn
11-16-2002, 07:43 PM
yeah, that should work. if it doesn't, try
if (typeof(window.opener) == "undefined") { ... }
beetle
11-16-2002, 08:57 PM
You're close...
if (typeof window.opener == 'undefined'){
// blah
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.