如何检测后退按钮 [英] How to detect the back button

查看:61
本文介绍了如何检测后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个使用以下代码的页面,因此,当您单击链接时,内容和URL会更改而不会刷新:

I've set up a page where I'm using the following code so when you click on a link the content and URL changes without refreshing:

window.history.pushState("object or string", "title", "/photo.php");

但是,当用户单击后退"按钮时,内容不会更改回.如何检测后退按钮?

But when a user clicks on the back button the content doesn't change back. How to detect the back button?

推荐答案

您可以使用 c0> ,就像这样.也会在使用导航按钮时将其触发.

You can use onpopstate, like this. It will be fired when the navigation buttons are used, too.

http://jsfiddle.net/54LfW/4/

window.onpopstate = function(e) { // executed when using the back button for example
    alert("Current location: " + location.href); // location.href is current location

    var data = e.state;
    if(data) {
        alert(JSON.stringify(e.state)); // additional data like your "object or string"
    }
};

这篇关于如何检测后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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