Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use onpopstate with IE?

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?

like image 957
doub1ejack Avatar asked Dec 06 '25 19:12

doub1ejack


2 Answers

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.

like image 124
doub1ejack Avatar answered Dec 08 '25 10:12

doub1ejack


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

like image 22
Linus Larsson Avatar answered Dec 08 '25 09:12

Linus Larsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!