簡體   English   中英

刪除警告框 - “你想離開這個網站嗎?”

[英]remove alert box - “Do you want to leave this site?”

我發現了很多這類問題,但沒有一個有幫助。

他們中的大多數給出了使用以下代碼的解決方案:

window.onbeforeunload = null;

我的問題是,當我在搜索框中鍵入內容並按 Enter 鍵時,jQuery 函數會運行並重定向到另一個 url。 但在重定向警告框出現之前->“您想離開此站點嗎?您所做的更改可能無法保存。”

這是我的 jquery 代碼

<input type="text" placeholder="Search" value="" id="SearchBox" />

    $('#SearchBox').keypress(function (e) {
            if (e.which == 13) {
                var search = $('#SearchBox').val();
                window.onbeforeunload = null;
                window.location.href = 'https://redirecturl';
                
            }
          });

也許你可以試試Disable alert();

它通過分配一個空函數來禁用警報框。

編輯

顯然你也可以覆蓋警報函數JavaScript: Overriding alert()

只需返回一個像這樣的空函數

(function(proxied) {
  window.alert = function() {};
})(window.alert);

在 JS 文件的末尾添加這個,每個 JS 文件,以防萬一你找不到 onbeforeunload 監聽器。

window.removeEventListener("beforeunload");

並檢查 HMTL 的 inline onbeforeunload,只需使用 ctrl+f 瀏覽 HTML 文件。

就是這樣,這將完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM