0

i want to open a popup window when user leaves tab or closes browser, the available option with me was window.open, but with that, it asks for popup blocker, if it is not enabled, i tried every possible ways to ignore popup-blocker but in vain, i tried onunload, onbeforeunload, but it does not works, i also tried to use modal dialog, before the tab/browser, i used it in onbeforeunload event, but it shows a browsers alert message, which i dont want, my simple requirement is, i need a popup window (if possible) or modal dialog, on the close of tab/browser, but should ignore popup-blocker, i guess with modal dialog, i dont need to worry about popup-blocker, because that wont be the new window, but how i would be handling it, while user closes tab/browser. If any of the above option is not possible, can anyone help me with any other possible alternative, or helpful articles.

Edited Question
i used the below script and with that i can open a new popup window on link click, without popup-blocker interfering in it

$('a[href=http://www.google.com]').click(function(){
    window.open(this.href);
    return false;
  });

then why its not possible for the closing events.

Abbas
  • 4,948
  • 31
  • 95
  • 161
  • possible duplicate of [Opening Popup from website without Popup Blocker catching it](http://stackoverflow.com/questions/12626051/opening-popup-from-website-without-popup-blocker-catching-it) – Quentin Sep 30 '12 at 15:34
  • possible duplicate of http://stackoverflow.com/questions/2587677/avoid-browser-pop-up-blockers/30555411 – Mohammed Safeer May 31 '15 at 08:36

3 Answers3

3

You can't - because that's what popup blockers do: they block popup windows (i.e. calls to window.open or invocations of target="_blank" links) unless it is directly in response to a user mouse action.

Opening popups when a browser window is closed was a common tactic of "pop-under" ads in the early 2000s, and it irritated users, that's why Firefox and IE6's popup blocker block them, and there is no way around it for you unless you ask the user to disable their popup blocker on your site (and I think you'll find most of them will have no idea how to do that).

What are you trying to accomplish anyway? What is the content of this popup that you want the user to see? What other approaches have you tried?

Dai
  • 141,631
  • 28
  • 261
  • 374
  • actually i want to display an ad, when user leaves the site, i have used onunload (for popup) and onbeforeunload (for modal dialog) event, but that displays the browser alert "Are you sure want to navigate away from this site, can i work around with modal dialog, do you have any idea for this. – Abbas Sep 27 '12 at 21:52
  • 2
    What you want to do is impossible. Users don't want adverts and the browsers block them - there is no way around it. – Dai Sep 27 '12 at 23:35
0

Exact duplicate of your previous question....

I'll post my answer for it here as well though...

Popup blockers are designed to prevent this behavior.

I would suggest using a modal window instead of an actual browser window. I don't think that these get blocked because they are opened within the page itself.

As for the event... You could do something like...

window.onbeforeunload = function whatever() {
       //Do code here for your modal to show up.
 }

If you are just trying ot give a warning or something you could do

window.onbeforeunload = function showWarning() {
       returnString = 'Whatever you want your message to be....';
       return returnString;
 }

By returning something in the onbeforeunload statement it will show a confirm box with your message in it.

Community
  • 1
  • 1
Cruril
  • 450
  • 4
  • 14
  • i tried onbeforeunload, i wrote the $("dialog").Show(); in it, but it never shows up, it directly closes the window – Abbas Sep 27 '12 at 22:41
  • I use onbeforeunload often in one of my applications. Is there any other code on your page that is interfering with it? And are you trying to open a modal window or just show an alert/confirmation window? – Cruril Sep 27 '12 at 22:48
  • alert and confirm box works, but the modal dialog stucks, can you give me any reference of the working application – Abbas Sep 27 '12 at 23:04
  • Unfortunately I don't know if you can do a modal. I have never used one in this situation. Here is a link to a [similar SO question](http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own) and also to the [MSDN article on onbeforeunload](http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx). I will try a quick example and see if it works for opening a modal. – Cruril Sep 28 '12 at 15:01
  • I was able to open a modal window. I use Telerik's Rad controls, so I don't know if its because of them or not. `radopen(null, null);` was able to open a blank modal with a popup blocker enabled, but it only opened when I had a returned a value as well, which will show the confirmation box. I believe that this is by design. So I don't know if there is a way to accomplish what you are trying to do without a confirmation or alert box. – Cruril Sep 28 '12 at 15:14
0

Popup blocker will not block the opening of popups if the javascript code which opens the popup is executed on user's direct action like click, keypress, drag, etc.

Vinay Jeurkar
  • 3,054
  • 9
  • 37
  • 56