Skip to content Skip to sidebar Skip to footer

Detecting If Browser Navigating To Another Page

I need to check if browser is navigating to another page or closing. My idea is; Create global variable var isNavigating = false; Bind click event to make isNavigating = true of e

Solution 1:

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...");
});

Solution 2:

Just bind to onunload event of the window.


Post a Comment for "Detecting If Browser Navigating To Another Page"