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

javascript - Can I "redirect" user in onbeforeunload? If can't, how to? - Stack Overflow

programmeradmin3浏览0评论

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

Attempts:

  1. I tried onunload, does not work

    window.onunload = function redirect(){...}
    
  2. I also tried another method, it doesn't work either:

    window.onbeforeunload = redirect(){...}
    
  3. <body onbeforeunload="return false; redirecty()">

The 3rd method, I want to cancel the onbeforeunload (means delay closing the browser), the I call the redirect function, window.confirm, if yes redirect, if no then close the browser. But it does not work as well.

Is there any other way?? Maybe prompt to let user select whether to redirect to new page when he/she close the browser? I'm running out of ideas...

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

Attempts:

  1. I tried onunload, does not work

    window.onunload = function redirect(){...}
    
  2. I also tried another method, it doesn't work either:

    window.onbeforeunload = redirect(){...}
    
  3. <body onbeforeunload="return false; redirecty()">

The 3rd method, I want to cancel the onbeforeunload (means delay closing the browser), the I call the redirect function, window.confirm, if yes redirect, if no then close the browser. But it does not work as well.

Is there any other way?? Maybe prompt to let user select whether to redirect to new page when he/she close the browser? I'm running out of ideas...

Share Improve this question edited Feb 16, 2022 at 15:30 Heretic Monkey 12.1k7 gold badges61 silver badges130 bronze badges asked Sep 11, 2009 at 4:07 kanayakikanayaki 3
  • 2 This will be irritating to the user[at least for me] to be redirected to a page when he tries to close the browser. – rahul Commented Sep 11, 2009 at 4:10
  • I am not forcing to redirect, I will prompt to let user to choose whether he want to be redirect or not, and it will only call once for each computer – kanayaki Commented Sep 11, 2009 at 4:15
  • The browser is still going to close anyway. What's the point of redirecting as the browser is closing? The user isn't going to see it anyway. – recursive Commented Sep 11, 2009 at 4:32
Add a comment  | 

4 Answers 4

Reset to default 4

Your onbeforeunload event needs to return a string (that doesn't equate to false), the browser will include this string in its own message displayed to the user.

window.onbeforeunload = function(){
        location.assign('http://www.google.com');
        return "go to google instead?";
    }

However, it's going to be really tricky to word your message in a way that the user would be able to understand what to do. And I'm not sure this is robust in every browser, I just tried it in Chrome, it worked, but I ended up with a tab I could not close! I managed to kill it via the Chrome task manager thankfully.

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.

If the user is trying to close the browser, then his intentions are pretty clear; he expects that the browser will close. Preventing that from happening, or causing anything else to happen in between the user clicking on 'close' and the browser closing is just a bad idea IMO. Is there a special reason for this? I mean, when I click on the 'close' button I expect that the browser will close, and should anything else happen, I would find that extremely annoying. I think I'm being reasonable enough. Am I? Who knows such things.

Why don't you try to entice the user to visit the other page in a less intrusive way? Like with a link or a banner?

The simple answer is no. If browsers allowed you to do more with the onbeforeunload/onunload events, this could be used pretty maliciously by anybody.

发布评论

评论列表(0)

  1. 暂无评论