views:

14

answers:

1

Can I get the updated URL before the page unloads.

I'd like to check if this URL is still the URL being used by the user during a refresh.I'd like to do something before the user changes to another page.

usecase:

[Normal] browser address bar: www.mypage.com/page1.html user clicks reload NO popup notification display Page reloads

[Abnormal] browser address bar: www.mypage.com/page1.html user modifies address bar: www.anotherpage.com POP UP notification displays before www.anotherpage.com is loaded

I'd like to know how to get the current value of the address bar before the page unloads I tried this code snippet:

var currPage;
window.onload = function(){
    currPage = document.location.href;
}
window.onbeforeunload = function (e){
    var nextLocation = document.location.href; // I assumed this value will update always. but NOT! :((
    if( currPage != nextLocation )
    {
        alert("You're leaving now??!");
    }
}

Please help me.

+1  A: 

document.location.href always gives the URL of the page from which your script is running. It doesn’t change.

You can’t get the URL of the page the user is navigating to. You could put click handlers on all your outgoing links in order to capture those cases, but you certainly can’t read an address that the user has typed into the address bar, for obvious privacy reasons.

Daniel Cassidy