JS determines whether the pop-up window is blocked program code

Source: Internet
Author: User

Most browsers have a pop-up screen program built in, and when the pop-up window is blocked, you should consider two possibilities. If the browser-built screen program blocks pop-up windows, then window.open () is likely to return null. At this point, you can determine if the pop-up window is blocked by detecting the returned value, as shown in the following example:

The code is as follows Copy Code

var Wroxwin = window.open ("http://www.111cn.net", "_blank");
if (Wroxwin = = null) {
Alert ("Bad!") Pop-up window is blocked ");
}

If browser extensions or other programs block pop-up windows, then window.open () usually throws an error. So to accurately detect whether a pop-up window is masked, you must encapsulate the window.open () call in a Try-catch block as you detect the return value, as follows:

The code is as follows Copy Code

var blocked = false;
try {
var Wroxwin = window.open ("http://www.111cn.net", "_blank");
if (Wroxwin = = null) {
blocked = true;
}
catch (ex) {
blocked = true;
}
if (blocked) {
Alert ("Bad!") Pop-up window is blocked ");
}

In any case, the above code can detect whether the pop-up window that was opened by calling window.open () is blocked. Note, however, that detecting whether a pop-up window is shielded is not, on the one hand, preventing the browser from displaying messages related to the blocked pop-up window.

If the browser blocks the pop-up instance, the instance returned by window.open () will be null, and then the focus () is thrown on the instance, which is bound to throw an exception. Here you can also simply compare the value of the instance to judge, without exception.

The code is as follows Copy Code

function Testpop () {

var mypop=window.open ("Http://www.111cn.net", "_blank", "Z-look=yes,menubar=no,toolbar=no, location=no,resizable= Yes, directotries=no,status=no,width=800px,height=600px,scrollbars=no ");

try{
Mypop.focus ()//
}catch (e) {
Alert (' blocked ');//show any information you would like the user to see
}

}

ps:ie6.0,7.0,8.0 test passed. After the Firefox,google Chrome set up the pop-up, this window still pops up, somehow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.