Forum Moderators: open

Message Too Old, No Replies

Events Generated from Back Button

...specifically when an anchor within the page is then loaded

         

premasagar

2:05 pm on Aug 20, 2006 (gmt 0)

10+ Year Member



A search gave me no clues...

I'd like to know what is the event that fires when a user clicks 'Back' on their browser to return to a different anchor on the same page? I need an event that I can catch with JavaScript.

To clarify, imagine this scenario:

1) User opens 'mypage.htm' (the 'load' event fires)

2) User clicks on a link internal to the page: 'mypage.htm#anchor1'.
Q1: Does an event fire here? Which one?

3) User clicks on another internal link, to 'mypage.htm#anchor2'.

4) User clicks 'back' button, to return to 'mypage.htm#anchor1'.
Q2: Does an event fire here? Which one?

5) User clicks 'back' button again, to return to 'mypage.htm'.
Q3: Does an event fire here? Which one?

The answers to all three questions may be identical - but I ask 3 times in case there is a difference. One thing I do know, in Firefox at least, the 'load' event does not fire again when going back to an already loaded page...

I'm very grateful for any insight.
Thanks,
Premasagar.

Robin_reala

3:27 pm on Aug 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onunload [developer.mozilla.org]?

whoisgregg

3:41 pm on Aug 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure if any event fires in the browser. You could set up a javascript that checks the hash value every x seconds and, if it changes, call some function.

premasagar

10:36 pm on Aug 20, 2006 (gmt 0)

10+ Year Member



Great! Thank you both for your replies.

@WhoisGregg - That's a good idea, though I'd have to make x seconds very small to achieve a nice effect in what I'm trying to do.

@Robin_Reala - Thank you for your one word response! The link you gave is about the unload event - that web page in turn gives another link about page caching in Firefox 1.5 [developer.mozilla.org], which is exceptionally useful. I didn't know that the browser cached in this way, though it does explain all the behaviour I've been seeing.

I could use the unload event. Another possibility, from the page linked above is about the pageshow event:

When a user navigates to a cached page, inline scripts and the onload handler do not run, since in most cases, the effects of these scripts have been preserved.

If the page contains scripts or other behaviors that fire during loading that you want to continue to execute every time the user navigates to the page, or if you want to know when a user has navigated to a cached page, use the new pageshow event.

:o)

Robin_reala

12:17 pm on Aug 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Makes sense, glad that helped.