Tell if a pop-up was blocked by checking the return value in JavaScript

Description

The following code shows how to tell if a pop-up was blocked by checking the return value.

Example


<!--from   w ww .j  a  va2 s. c o m-->
<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var blocked = false;
try {
var myWin = window.open("http://www.java2s.com", "_blank");
if (myWin == null){
blocked = true;
}
} catch (ex){
blocked = true;
}
if (blocked){
document.writeln("The popup was blocked!");
}

</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Tell if a pop-up was blocked by checking the return value in JavaScript