檢測瀏覽器是否導航到另一個頁面

[英]Detecting if browser navigating to another page


I need to check if browser is navigating to another page or closing. My idea is;

我需要檢查瀏覽器是否導航到另一個頁面或關閉。我的想法是;

  1. Create global variable var isNavigating = false;
  2. 創建全局變量var isNavigating = false;

  3. Bind click event to make isNavigating = true of every anchor which will navigate to another page
  4. 綁定點擊事件使每個錨點的isNavigating = true將導航到另一個頁面

  5. Check if isNavigating true on body's unload event.
  6. 檢查body的卸載事件是否為isNavigating為true。

Any other ideas? Recommendations?

還有其他想法嗎?建議?

2 个解决方案

#1


8  

You can do this by the following script.

您可以通過以下腳本執行此操作。

<script type="text/javascript"> 
     window.onbeforeunload = function(){ return;} 
</script> 

However if you plan to cancel the navigaion, just don't bother. It's not possible as far as i know.

但是,如果您打算取消導航,請不要打擾。據我所知,這是不可能的。


Code below checks if the user has clicked a link.

下面的代碼檢查用戶是否點擊了鏈接。

var checkAnchorClick = false;

$(document).ready(function () {
    $("a").live("click", function () {
        checkAnchorClick = true;
    });
});

$(window).unload(function () {
    if (checkAnchorClick)
        alert("User clicked a link...");
    else
        alert("Something else...");
});

#2


0  

Just bind to onunload event of the window.

只需綁定到窗口的onunload事件。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/07/07/7202591b560d611d814a3ab519617871.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com