onpopstate处理器 - 阿贾克斯后退按钮 [英] onpopstate handler - ajax back button

查看:132
本文介绍了onpopstate处理器 - 阿贾克斯后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用pushState来创建我的网站上的Ajax内容有意义的URL。我希望当用户点击前进或后退按钮刷新Ajax内容。我有问题,但是因为铬(正确)impliments onpopstate双方的历史穿越(前进/后退按钮)和结束(网页加载)。我想创建一个onpopstate处理程序,页面加载和历史穿越区分,因为我不希望我的刷新Ajax内容,如果它已经刚刚成功加载。任何人可以帮助我与这两个之间的区别?

I'm using pushState to create meaningful urls for ajax content on my site. I want to refresh the ajax content when the user hits the forward or back buttons. I'm having problems however as chrome (correctly) impliments onpopstate on both 'history traversal' (forward/back buttons) and 'the end' (page load). I'd like to create an onpopstate handler that differentiates between page load and history traversal, as I don't want to refresh my ajax content if it's already just been loaded successfully. Can anybody help me with differentiating between the two?

window.onpopstate = function (event) {
    // If : Forward/back button pressed
        // Reload any ajax content with new variables
    // Else : Page load complete
        // Do nothing - content already loaded and correct
}

请参阅<一href="http://$c$c.google.com/p/chromium/issues/detail?id=63040">http://$c$c.google.com/p/chromium/issues/detail?id=63040对铬和onpopstate更多细节。

See http://code.google.com/p/chromium/issues/detail?id=63040 for more details on chrome and onpopstate.

感谢

推荐答案

一个方法是初始化变量设置为false在页面加载,然后在你的popstate事件,检查变量。如果它仍然是假的,它设置为true,什么也不做。从那时起,所有的其他popstate事件应该只是从前进/后退的事件。

One way would be to initialize a variable to false on page load, and then on your popstate event, check that variable. If it's still false, set it to true and do nothing. From then on all of your other popstate events should be only from back/forward events.

var loaded = false;

window.onpopstate = function(e) {
    if (!loaded) {
        loaded = true;
        return;
    } else {
        // run your stuff...
    }
}

这篇关于onpopstate处理器 - 阿贾克斯后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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