using onbeforeunload event, url change on selecting stay on this page

后端 未结 6 1284
别那么骄傲
别那么骄傲 2020-11-29 06:03

Rewriting the question -

I am trying to make a page on which if user leave the page (either to other link/website or closing window/tab) I want to show the on

6条回答
  •  眼角桃花
    2020-11-29 06:44

    It's better to Check it local.
    Check out the comments and try this: LIVE DEMO

    var linkClick=false; 
    document.onclick = function(e)
    {
        linkClick = true;
        var elemntTagName = e.target.tagName;
        if(elemntTagName=='A')
        {
            e.target.getAttribute("href");
            if(!confirm('Are your sure you want to leave?'))
            {
               window.location.href = "http://google.com";
               console.log("http://google.com");
            }
            else
            {
                window.location.href = e.target.getAttribute("href");
                console.log(e.target.getAttribute("href"));
            }
            return false;
        }
    }
    function OnBeforeUnLoad () 
    {
        return "Are you sure?";
        linkClick=false; 
        window.location.href = "http://google.com";
        console.log("http://google.com");
    }
    

    And change your html code to this:

    
        try it
    
    

提交回复
热议问题