亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在瀏覽器中檢測退按鈕單擊

在瀏覽器中檢測退按鈕單擊

白衣非少年 2019-07-22 19:32:31
在瀏覽器中檢測退按鈕單擊我必須檢測用戶是否單擊了后退按鈕。為此我用window.onbeforeunload = function (e) {}如果用戶單擊“后退”按鈕,它就會工作。但是,如果用戶單擊瀏覽器的F5或重新加載按鈕,也會觸發此事件。我該怎么解決這個問題?
查看完整描述

3 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

請嘗試此操作(如果瀏覽器不支持“onpreunload”):

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

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

    $(window).on('popstate', function() {
      var hashLocation = location.hash;
      var hashSplit = hashLocation.split("#!/");
      var hashName = hashSplit[1];

      if (hashName !== '') {
        var hash = window.location.hash;
        if (hash === '') {
          alert('Back button was pressed.');
        }
      }
    });

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


查看完整回答
反對 回復 2019-07-22
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

我知道最好的方法

         window.onbeforeunload = function (e) {
            var e = e || window.event;
            var msg = "Do you really want to leave this page?"

            // For IE and Firefox
            if (e) {
                e.returnValue = msg;
            }

            // For Safari / chrome
            return msg;
         };


查看完整回答
反對 回復 2019-07-22
  • 3 回答
  • 0 關注
  • 389 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號