JavaScript或jQuery浏览器后退按钮点击检测器 [英] JavaScript or jQuery browser back button click detector

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

问题描述

有人可以分享经验/代码我们如何检测浏览器后退按钮点击(对于任何类型的浏览器)?

Could someone please share experience / code how we can detect the browser back button click (for any type of browsers)?

我们需要满足所有浏览器的需求't支持HTML5

We need to cater all browser that doesn't support HTML5

推荐答案

'popstate'事件仅在您推送之前有效。所以你必须这样做:

The 'popstate' event only works when you push something before. So you have to do something like this:

jQuery(document).ready(function($) {

  if (window.history && window.history.pushState) {

    window.history.pushState('forward', null, './#forward');

    $(window).on('popstate', function() {
      alert('Back button was pressed.');
    });

  }
});

对于浏览器向后兼容性,我建议:history.js

For browser backward compatibility I recommend: history.js

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

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