Search code examples
internet-explorerbrowser-history

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?


Solution

  • 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.