Can I use onpopstate with IE?

馋奶兔 提交于 2019-12-11 03:44:29

问题


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?


回答1:


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.




回答2:


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




回答3:


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




回答4:


this solved me:

if( navigator.userAgent.toLowerCase().indexOf('msie ') ) window.onhashchange = locate else window.onpopstate = locate



来源:https://stackoverflow.com/questions/11232745/can-i-use-onpopstate-with-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!