views:

99

answers:

2

How to get informed if your PopUp window was blocked BY pop up blocker? (CODE EXAMPLE NEEDED)

+4  A: 

With

window.open (URL, windowName[, windowFeatures]);

you open a new popup and as URL you put some html page. In that popup'ed page you write some JS in which you can use window.opener variable so you can has an access to all JS from your main page. Then you can in popuped page set some flag - for example:

index.html

<html>
  <body>
    <script type="text/javascript">
      var opened = false;
      window.open('popup.html');
      // and here some loop in mooTools/jQuery/or
      // something to look up for variable changes
    </script>
  </body>
</html>

popup.html

<html>
  <body>
    <script type="text/javascript">
      window.opener.opened = true;
    </script>
  </body>
</html>

I'll do this that way.

Edit

And here is another way using PHP:

In your popup.php you can set some flag in session

$_SESSION['opened'] = true;

And in index.php you should write something to AJAX requesting to ana action which will returns you a value from $_SESSION.

hsz
With that loop it is a free idea - you can name your popup and add event `domready` for it too.Also I thing it is important to set some timeout.
hsz
@haz how to set timeout?
Blender
It depends of js lib you use.
hsz
I use jQuery lib!)
Blender
Google it - http://jquery.offput.ca/every/ ;)
hsz
A: 

The short answer is that you can't unless you really want to punish your users.

Popups are typically generated on the client side using JavaScript, so therefore you cannot know if the popup was blocked from the server unless you are watching to compare requests for page content versus requests for popup contents from the same IP. That method produces an incredible number of false-positives and false-negatives though because if a user refreshes the page they may not get the popup a second time, but they may have gotten it the first time. Or a popup window could have generated and content in that window can then load independently of content in the rest of the page. As a result the only rational solution is entirely unreliable.

So, what you can do is write a JavaScript test to measure if a popup window generated as it should have from the firing JavaScript. When the firing JavaScript fires, but the popup window does not immediately exist then you would have to fire off AJAX to send you an alert so that you are dynamically notified the popup failed to load. If I ever saw code like this, however, I would blacklist your entire domain from my enterprise and alert major security firms that you are either a malicious website or have been compromised.