鍍金池/ 問答/HTML/ JS如何禁用瀏覽器自帶后退鍵??

JS如何禁用瀏覽器自帶后退鍵??

需要的效果就是瀏覽器自帶的返回鍵不可點(diǎn)擊,或者點(diǎn)擊無效,置灰都可以

回答
編輯回答
遲月

使用onbeforeunload比較合適吧,彈出一個(gè)提示框讓用戶確認(rèn),這樣用戶以任何方式關(guān)閉此頁面前都需確認(rèn)

window.onbeforeunload = function (e) {
  e = e || window.event;
  // 兼容IE8和Firefox 4之前的版本
  if (e) {
    e.returnValue = '關(guān)閉提示';
  }
  // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
  return '關(guān)閉提示';
};

window.onbeforeunload

2017年7月19日 22:16
編輯回答
青瓷

還有種做法,將目標(biāo)頁的鏈接修改為:<a href="#" onclick="location.replace('目標(biāo)頁.html')">目標(biāo)頁.html</a>

2018年1月21日 17:40
編輯回答
裸橙

jQuery(document).ready(function ($) {
    if (window.history && window.history.pushState) {
      $(window).on('popstate', function () {
          window.history.forward(1);
      });
    }

   });

2017年1月11日 10:34
編輯回答
孤客
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});
2018年5月7日 09:29