Javascript, Can I “redirect” user in onbeforeunload? If cant, how to

后端 未结 4 788
Happy的楠姐
Happy的楠姐 2020-12-06 17:28

Is it possible to redirect to another page when the user closes the browser?

Attempts:

  1. I tried onunload, does not work

    window.onunlo         
    
    
            
4条回答
  •  天涯浪人
    2020-12-06 18:11

    It's not without it's faults but it works

    window.onbeforeunload = function(){
            window.open("http://www.google.com","newwindow");
            return "go to google instead?";
    }
    

    This will open a new window as a popup to the address you selected when the user closes the page, though it is limited by any popup blockers the browser may implement.

提交回复
热议问题