簡體   English   中英

如何在jquery中檢測瀏覽器的后退按鈕以及是否打開任何對話框將其關閉?

[英]how to detect back button of browser in jquery and if any dialogue is open close it?

我想要做的是,如果在打開任何其他彈出窗口時按返回按鈕,則應該關閉該彈出窗口並重新加載頁面。

我在下面嘗試過。 但只使用鍵盤后退按鈕,而不使用瀏覽器后退按鈕。 誰能幫我解決這個問題

$(document).ready(function() {

     $(document).bind("keydown keypress", function(e){
            if( e.which == 8 ){ // 8 == backspace
                alert("back button");
            }
        });
}

用於檢測瀏覽器后退並關閉彈出窗口的代碼段。

var path = 'pageURL';
history.pushState(null, null, path + window.location.search);
window.addEventListener('popstate', function (event) {
    //Code to close the pop up
    history.pushState(null, null, path + window.location.search);
});

如果您希望它停止導航至此:

$(document).bind("keydown", function(e) {

    // if a object has focus
    // inputs, textarea, etc.
    if($(":focus").length != 0) {
        return;
    }

    // else don't allow the press
    if(e.keyCode == 8) {
        parent.postMessage("message");
        window.close();
        e.preventDefault();
    }
});

對我來說,它捕獲並禁用了瀏覽器。 您可以使用postMessage在Windows之間進行通信

有關在Windows之間進行通信的更多信息。 https://davidwalsh.name/window-iframe

暫無
暫無

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

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