2

I want to open a popup when the user closes the last tab of the current site. The problems I have is that

  • when the browser prompts the user (onbeforeunload returning a non null value), then the popup is blocked by the browser (see case 1 below)
  • when the browser does not prompt anything to the user (onbeforeunload not returning anything), then the popup is not opened at all and seemed to be totally ignored by the browser (why is that?) (see case 2 below)

Case 1: prompt and popup blocked (code from this answer)

window.onbeforeunload = function (e) {
    e = e || window.event;

    window.open('http://localhost', '_blank');

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Sure?';
    }

    // For Safari
    return 'Sure?';
};

Case 2: no prompt, no popup, page closing "normally"

window.onbeforeunload = function (e) {
    e = e || window.event;

    window.open('http://localhost', '_blank');
};
Community
  • 1
  • 1
user7890
  • 147
  • 2
  • 11
  • Browsers block those on purpose, as they've been overused by crappy advertisement engines, or to prevent users from actually leaving the pages they want to leave. – spectras Aug 11 '15 at 12:18

2 Answers2

2

From the specification:

An algorithm is allowed to show a popup if any of the following conditions is true

The task in which the algorithm is running is currently processing an activation behavior whose click event was trusted.

The window closing is not a click event.

The task in which the algorithm is running is currently running the event listener for a trusted event whose type is in the following list:

unload is not in that list.

The task in which the algorithm is running was queued by an algorithm that was allowed to show a popup, and the chain of such algorithms started within a user-agent defined timeframe.

For example, if a user clicked a button, it might be acceptable for a popup to result from that after 4 seconds, but it would likely not be acceptable for a popup to result from that after 4 hours.

Again, no. The task wasn't queued.

It is not possible to open a popup in response to a user trying to leave your site. It would enable too many harmful behaviours (click on this advert, quit, popup, you must click on this advert, loop).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Here's my code:

   var popWindow = true;
   window.onbeforeunload = function() { 
       if(popWindow == true) {
            popWindow = false;
            return "Sure?"; 
       }
  }

http://jsfiddle.net/anam/su8dznoa/

anam
  • 216
  • 1
  • 2
  • 14
  • That doesn't open a popup at all. – Quentin Aug 11 '15 at 12:13
  • Sure it does, check my fiddle. You may have missed it. – anam Aug 11 '15 at 12:14
  • No, it doesn't. The fiddle has the same code, and there is nothing about opening popup windows in there at all. Even if that wasn't the case, answers are supposed to be in the answer and not just linked to. – Quentin Aug 11 '15 at 12:15
  • It is working on my side. I am linking to. I answered and used fiddle to prove it. – anam Aug 11 '15 at 12:18
  • So why doesn't a window open when I close the fiddle window? You seem to be confusing "opening a new window" with "setting a variable to false". – Quentin Aug 11 '15 at 12:22
  • I am seeing pop up when i closed my window. not sure why can't get? – anam Aug 11 '15 at 12:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86697/discussion-between-anam-and-quentin). – anam Aug 11 '15 at 12:28
  • I think you might be confusing the modal dialog with the text "Sure?" with the popup window displaying the HTML document located at `http://localhost/`. – Quentin Aug 11 '15 at 12:31
  • Note the use of `window.open` in the question. That is significant and what is generally meant by the term "popup" – Quentin Aug 11 '15 at 12:34