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