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

后端 未结 6 1277
别那么骄傲
别那么骄傲 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:32

    Try the following, (adds a global function that checks the state all the time though).

    var redirected=false;
    $(window).bind('beforeunload', function(e){
        if(redirected)
            return;
        var orgLoc=window.location.href;
        $(window).bind('focus.unloadev',function(e){
            if(redirected==true)
                return;
            $(window).unbind('focus.unloadev');
            window.setTimeout(function(){
                if(window.location.href!=orgLoc)
                    return;
                console.log('redirect...');
                window.location.replace('http://google.com');
            },6000);
            redirected=true;
        });
        console.log('before2'); 
        return "okdoky2";
    });
    
    $(window).unload(function(e){console.log('unloading...');redirected=true;});
    

提交回复
热议问题