我可以在IE上使用onpopstate吗? [英] Can I use onpopstate with IE?

查看:1004
本文介绍了我可以在IE上使用onpopstate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前(很高兴)使用jquery将ajax请求函数绑定到非IE浏览器中的window.onpopstate事件。但是,IE从未命中我的 doAjax 函数。

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;

任何人都知道是否有办法让IE 8/9以某种方式玩得很好?

Anyone know if there's a way to make IE 8/9 play nice somehow?

推荐答案

我到达的解决方案是绑定 onpopstate onhashchange 到所需的处理程序。

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;

我正在使用History jquery库来更新url,因为我对页面进行了ajax更改。不幸的是,可以预见的是,IE还没有赶上,似乎没有办法改变JS的URL。历史可以追溯到在IE中更新url的哈希状态,所以这最初的目标是将url状态更改绑定到IE中的处理程序。

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

编辑:
正如@linus指出的那样,我们应该慈善并避免使用浏览器检测为IE提供了改革的机会。

As @linus points out, we should be charitable and avoid browser detection to give IE an opportunity for reform.

这篇关于我可以在IE上使用onpopstate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆