I'm currently (happily) using jquery to bind a ajax request function to the window.onpopstate event in non-IE browsers. However, IE never hits my doAjax
function.
// Bind a function to the popstate event to execute ajax requests
// this allows request to occur on back/fwd browser navigation
window.onpopstate = doAjax;
Anyone know if there's a way to make IE 8/9 play nice somehow?
The solution I have arrived at is to bind both onpopstate
and onhashchange
to the desired handler.
// Popstate: load ajax
window.onpopstate = handlePageWipe;
// And onhashchange for IE
if( jQuery.browser.msie ) window.onhashchange = handlePageWipe;
I am using the History jquery library to update the url as I make ajax changes to the page. Unfortunately, and predictably, IE hasn't caught up and there seems to be no way to alter the url with JS. History falls back to updating the url's hash state in IE, so this serves my initial goal of binding a url state change to a handler in IE.
Of course this opens up another can of worms because I now have to handle both url changes and hash changes. Ah well, so it goes...
EDIT:
As @linus points out, we should be charitable and avoid browser detection to give IE an opportunity for reform.
I would prefer to do like this:
if (window.onpopstate != undefined) {
window.onpopstate = locate;
} else {
window.onhashchange = locate;
}
so if "msie" add this functionality you do not depend on old code
I'm on a project that uses jQuery history plugin, copyrighted by Takayuki Miwa. The client code initialises the library with $.history.init(callback, ...)
.
That callback is invoked each time you navigate to/refresh the page, and also each time the page content is updated by means of AJAX, whether for clicks, history back, history forward.
If you are using that same library, you could do the same in order to have code invoked when page content changes, thus dropping the onpopstate/onhashchange thing.
If you are using the more recent History.js from B A Lupton plugin, there's a statechange
event you can bind to.
Also, bear in mind that even onhashchange
is supported "only" from IE8.
HTH
this solved me:
if( navigator.userAgent.toLowerCase().indexOf('msie ') )
window.onhashchange = locate
else
window.onpopstate = locate