最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to capture the destination URL in onbeforeunload event - Stack Overflow

programmeradmin2浏览0评论

I want to use onbeforeunload to give a message to users before leaving certain pages. Is it possible to know which URL they are supposed to jump to at the onbeforeunload event?

I want to use onbeforeunload to give a message to users before leaving certain pages. Is it possible to know which URL they are supposed to jump to at the onbeforeunload event?

Share Improve this question edited Apr 10, 2023 at 9:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 3, 2010 at 9:32 NirNir 25.4k26 gold badges84 silver badges120 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Is it possible to know which url they are supposed to jump to at the onbeforeunload event?

No, definitely not. The onbeforeunload event tells you only that the page is about to be unloaded, but not why.

It depends on how the user is leaving the page. If they are typing an url in the address bar - then you're out of luck. As far a I know there's no way to capture the url of an address bar jump.

If they are clicking on a link contained somewhere on the page - that you can use the click event to capture the url and then decide how you want to handle things.

I posed a similar question
How can i get the destination url of a link on the web page in the javascript onbeforeunload event?
because I had a project to fix a wholesale order form. During the process of filling out an order my client's customers would go back to the catalog to check on a product's detail page and loose the current information on their order form.
By using the code below (which uses JQuery though I'm sure you could create the same thing in pure Javascript if you had to) I could tell when the user clicked a link that would leave the order form and then give them the option of opening the link in a new window/tab or loading the url in the current window and loosing their form data, or just returning to the form. This code works with all of the major browsers, at least their more recent versions.

$('body a').click(function(e) {     
    //if link references a page element
    if ($(this).attr('href').charAt(0)=="#") {
        return;
    }

    //check if link is to same window    
    var pathname = window.location.pathname;
    var pathname2 = $(this).attr('href');
    pathname2 = pathname2.replace(/^.+\.\//, '');
    if (pathname.indexOf(pathname2) >= 0) {
        //link clicked is contained on same page
        //prevent page from getting reloaded & losing data
        e.preventDefault();
        e.stopImmediatePropagation();
        e.stopPropagation();    
        return;     
     }

    //link clicked on is another page
    if (hasMerchandise) {  //var to indicate user has items on order form       
        //give user options: leave page, open link in other page, stay, etc.
        // $('.popupForm-handleExitRequest').click();   //roll your own code    

        //prevent page from getting reloaded & losing data
        //in case user wants to cancel page change or open link in another window
        e.preventDefault();
        e.stopImmediatePropagation();
        e.stopPropagation();            
    } else {
        //load new page
        $(this).removeAttr('target');
    }       
});
发布评论

评论列表(0)

  1. 暂无评论