I have obtained the following script which only lets a page load if the visitor has been to the previous page, if they haven’t it sends the visitor to the appropriate page. However, it only works in Mozilla. Can anyone help me adapt this script to work in Internet Explorer?

<SCRIPT LANGUAGE="JavaScript" > 
<!-- Begin
var requiredfrom = "http://domain.com/somepage.htm"; //  required prev. page
if (document.referrer.indexOf(requiredfrom) == -1) {
alert("You must logon first...");
window.location=requiredfrom;
}
//  End --> 
</script>

Cheers,
NuGG

Recommended Answers

All 10 Replies

If you need an example of this script in action click here.

P.S. When I try this script in IE I receve the "must logon first" error regardless of visiting the previous page or not.

I'm using Internet Explorer and it worked for me!

How odd. What version of IE are you using? I've tried it on multiple computers with IE 6.0 and it didnt work on any of them...

Does this javascript work in IE for anyone else?

I'm using Internet Explorer and it worked for me!

Its quite odd that it didnt work for me in IE but it works for you. Could it be something to do with the history settings in IE, or would that not make any difference?

The script works perfectly every time in firefox so i dont understand why it shouldnt work in IE.

document.referrer may contain the address of the previous page or it may not. There are a number of factors that can affect this such as browser settings, firewall settings, IE also suppresses the value if the prior page was on a secure server, etc. If this happens then you have no way to tell what page your visotir was on previous to coming to your page.

...but it did work for me, IE and FireFox.

You have successfully logged on...
Click here to continue.

Note: I take no responsibility for your presence on this site during school. You are here out of your own free will, and if you get your internet and email restricted its your own fault.

Then when I clicked the word "here" I got the you must log on message in a popup.

Whether the script works or not can depend on the browser and firewall settings on individual computers. That would be the reason some people say it works and others say it doesn't. You can't rely on document.referrer containing anything as there are too many different ways it can be turned off.

Ok Im going to forget that idea then. Thanks for your help.

A client side solution means the client has to load the page first, you absolutely need a server side solution.
PHP session variables I think would be the way to go, when a page is requested, your PHP on the server would determine what was served up to the requestor.
I have not used PHP sessions yet so I could not explain it's use, but have been reading about it so I can implement it in the near future.
Here are some links that might help:
http://us.php.net/manual/en/ref.session.php
http://www.sitepoint.com/article/users-php-sessions-mysql

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.