javascript - Beforeunload is not working to redirect user to another page, how can I redirect user to another pageurl when he at

admin2025-04-10  0

My following code isn't working to redirect the user to another page:

$(window).on('beforeunload',function(){
                        window.location.href=";
                    }) ;

I want the user to be redirected to another page when he attempts to close the tab. What's the alternative and appropriate way to achieve this?

My following code isn't working to redirect the user to another page:

$(window).on('beforeunload',function(){
                        window.location.href="http://www.google.;
                    }) ;

I want the user to be redirected to another page when he attempts to close the tab. What's the alternative and appropriate way to achieve this?

Share Improve this question asked Jul 19, 2013 at 13:14 Umer HassanUmer Hassan 1,1476 gold badges16 silver badges23 bronze badges 5
  • 3 As an aside, this isn't a great thing to do. There are very few cases where this behavior is appreciated (e.g. half pleted email / blog post and you're closing the window), and even then it's usually to warn the user and not to send them to another page. Not even sure a redirect is allowed at this stage. – JohnP Commented Jul 19, 2013 at 13:17
  • 2 You know that that's a near-hostile act against the user, right? – Ben Barden Commented Jul 19, 2013 at 13:18
  • as far as i know, this isnt possible! – reyaner Commented Jul 19, 2013 at 13:19
  • Here is a good post for this: stackoverflow./questions/6063522/jquery-beforeunload – reyaner Commented Jul 19, 2013 at 13:20
  • I don't know about anyone else, but when I close a tab, I don't expect to be redirected to Google. – michaelb958--GoFundMonica Commented Jul 19, 2013 at 13:30
Add a ment  | 

2 Answers 2

Reset to default 4

*Don't do it*

But it is possible with the user's permission; you can achieve something like this (took me a while to find a website that was happy in a frame)

window.onbeforeunload = function () {
    window.setTimeout(function () { // escape function context
        window.location = 'http://bbc.co.uk';
    }, 0);
    window.onbeforeunload = null;   // necessary to prevent infinite loop
                                    // that kills your browser
    return 'Press "Stay On Page" to go to BBC website!';
        // pressing leave will still leave, but the GET may be fired first anyway
}

Demo

I don't think this is possible.

There are some things you can can do in this event but that is severely limited due to spammers back in the day. They used to have animated text in the window statusbar which would obscure link href's so you would be clicking blind and open tons of windows when you tried to leave so that you were essentially trapped on the site.

This got to be such a problem that as far as I recall it was one of the "features" that Firefox bragged about solving when it first launched.

It was toned down to being able to beg them to stay with a dialog box but then that was abused as people worded it like official system messages and tricked people.

Now most browsers let you request a "stay on page / leave page" dialog but dont give you any control over the wording.

Here are some docs that list your options:

  • https://developer.mozilla/en-US/docs/Web/API/window.onbeforeunload
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744274968a239201.html

最新回复(0)