Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Javascript to function differently if page loaded by back button

Status
Not open for further replies.

ArcAnjel

Programmer
Feb 9, 2005
13
US
As the subject says, I have a page that needs to run differently if the page is loaded by a link or if it is loaded by the back button on the browser. I want to test for this in the Javascript.

Pseudocode:
if (loaded by back button) {
Do this;
}
else if {
Do this instead;
}


Any ideas?

-Carl
 

I would say that you would have to use cookies for this. Consider this scenario:

1. Clicking a link will set a value in the cookie before loading the page
2. When the page loads, if the value in the cookie is set, run code "a". If not, run code "b".
3. Clear the cookie value.

This will ensure that if a user navigates away from the page, and then back using the back button, code "b" will always run.

Of course, the success of this scheme needs 2 things:

1. For any link to this page to run through the cookie setting code.
2. For users to have cookies enabled for your site.

Personally, I'd re-architect things so this isn't an issue. You can never rely on users having cookies switched on (I never do by default).

Hope this helps,
Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top