views:

46

answers:

2

I change the bookmark in the url when the page is loaded. That way, when the user clicks the back button of the browser, the browser will not actually go back, but will instead change the bookmark. I can then detect the bookmark change and do something else as a reaction to the user pressing the back button.

My problem is to find the current url, including any bookmark/hash changes. It works in all browsers, using a combination of the following, but not in IE8:

document.URL
location.href
window.location.hash

and the window.onhashchange

As it doesn't work for IE8, could anyone possibly point me in the right direction towards how I can detect the bookmark/hash change in IE8?

A: 

Look at jquery-bbq, as it implements the hashchange and makes it work in IE6-IE8 and possibly IE9 now. You could probably entirely rely on it instead of your custom code.

meder
Hi meder - thanks a lot. I will try it out, but however strange it sounds, jquery is not easily implementable in my project. Therefore I'm still interested in knowing the core javascript function that can be used, which must be what jquery is also using.
Niels Brinch
+1  A: 

According to the author of the jQuery hashchange plugin, IE8 supports binding to the window.onhashchange event out of the box.

Perhaps you could try using Ben's plugin, which is intended to enable hash change detection on older browsers.

EDIT: My tests showed that the event is not firing in IE8. Then I found the following comment in above plugin's source code:

// Note that IE8 running in
// IE7 compatibility mode reports true for 'onhashchange' in window, even
// though the event isn't supported, so also test document.documentMode.

Apparently, I'm running in documentMode 5 which is quirks mode. I bet it works in IE8 standards mode only. Regardless, you should be able to implement the same JS code that Ben used.

Rob Sobers