开发者

How to get informed if your PopUp window was blocked?(HTML JS PHP)

How to get informed if your PopUp window was blocked BY pop up blocker? (CODE EXAM开发者_如何学PythonPLE NEEDED)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜