简体   繁体   English

如何检测浏览器后退按钮点击,兼容所有浏览器

[英]How to Detect Browser back button click, which is compatible for all browser

I tried below codes but that is not consistent.我尝试了以下代码,但这并不一致。

if (performance.navigation.type == 2) {
    alert("Back button clicked");
}

and,和,

if (performance.getEntriesByType("navigation")[0].type === 'back_forward') {
   alert("Back button clicked");
}

Please suggest if any other possible ways to Detect back button in browsers.请建议是否有任何其他可能的方法来检测浏览器中的后退按钮。

Try history尝试历史

(function() { (功能() {

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

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

})(); })();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM