Forum Moderators: open

Message Too Old, No Replies

Detecting [Back] and [Forward] events

onBack, onForward?

         

OXiGEN2

5:40 am on Dec 3, 2003 (gmt 0)

10+ Year Member



Search Keywords (any): onBack onForward onStop
Search Results: 0

Hi,

First post on this board. Very nice forum :)

I'm trying to detect when the user clicks the [Back] or [Forward] button in their browser. Supposedly onBack and onForward can do that. However, the following code was unsuccessful on WinXP/IE6 + NS7.1:

<body onBack="alert('user pressed back');">

Also, tried placing the event inside <html>, <head>, and <script></script> to no effect. How/where should I use these handlers?

Furthermore, I need to detect the URL (complete with #hash) the user will be taken to. I have DIVs that are shown/hidden via JS based on the #hash. It'd be nice to be able to call the function on Back/Forward events to make navigation more intuitive for the user.

Thanks!

dmorison

6:53 am on Dec 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doesn't the onUnload event fire on leaving a page via the forward or back buttons?

OXiGEN2

11:05 am on Dec 3, 2003 (gmt 0)

10+ Year Member



onUnload works when completely leaving a page. I need to detect #hash (#anchor) changes *within* the same page:
* page.html#anchor1
* page.html#anchor2

Here's a page demonstrating my problem -- [oxigen.f2o.org...]
Click on "Tidal Wave", "Decoy", "Pure Water", etc. Now, click [Back] and [Forward]. Do not click "< Virtue's Disciple" or "Dragon Knight >"; those leave the page.

You'll see the page simply stays in the last state when you clicked an internal link. However, the URL bar changes between ...#s1 ...#s2 ...#s3 and so on. What I'm trying to do is to update which DIVs are displayed when [Back] and [Forward] are used, same as when clicking the links inside the page. This can be easily done, if I could simply detect the full content of the URL bar after [Back] or [Forward] is pressed.

On a straight HTML doc, with straight #anchors, [Back] and [Forward] work as you'd expect. The page will scroll to the referred #anchor. However, I'm using #anchors as markers for processing by JS. DIVs on the same page are hidden and shown based on the #hash.

Hope that clears it up a bit more. Thanks for taking a look :)

OXiGEN2

4:38 am on Dec 4, 2003 (gmt 0)

10+ Year Member



Still searching for answers... Found this in a DevEdge archived faq from 5/26/99:

"2.10: Can I detect the back button?
Unfortunately, no! However, the event handlers onBack and onForward are planned in a later version of Communicator."

A search for "onBack" on the current devedge.netscape.com site returned no results. Does this mean support was never implemented, and never will be? Thus, all references to onBack and onForward are invalid? So, there is no solution to my problem?

Sorry for bothering you all so much about this. It's the last major bug I need to squish =/

DrDoc

5:30 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The value of location.href should change. You can always check that I guess.

dmorison

6:02 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Failing that; presumably the #ref only changes when your user clicks on something; so just trap the clicks...

OXiGEN2

6:19 am on Dec 4, 2003 (gmt 0)

10+ Year Member



Hmm.. that's an interesting idea. Is there anyway to tell when the click event occurs over the browser's Back and Forward buttons?

Right now, I'm beginning to think it's necessary to have a looping timer set to 1 second intervals simply to detect if the URL#anchor has changed. Very messy and inefficient, but the only supported solution so far =/

DrDoc

6:27 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Even if you could detect if the person clicks the browser's back and forward buttons, what if they use Alt-Left or Alt-Right... or even Backspace to navigate... or mouse gestures? Yes, the location.href check seems to be the only solution.