<p>Occasionally, I've come across a webpage that tries to pop open a new window (for user input, or something important), but the popup blocker prevents this from happening.</p>

<p>What methods can the calling window use to make sure the new window launched properly?</p>

[{'answer_id': 2917, 'author': 'omar', 'author_id': 453, 'author_profile': 'https://Stackoverflow.com/users/453', 'pm_score': 9, 'selected': True, 'text': "<p>If you use JavaScript to open the popup, you can use something like this:</p>\n\n<pre><code>var newWin = window.open(url);             \n\nif(!newWin || newWin.closed || typeof newWin.closed=='undefined') \n{ \n    //POPUP BLOCKED\n}\n</code></pre>\n"}, {'answer_id': 13652829, 'author': 'Kevin B', 'author_id': 886103, 'author_profile': 'https://Stackoverflow.com/users/886103', 'pm_score': 5, 'selected': False, 'text': '<p>Update: \nPopups exist from really ancient times. The initial idea was to show another content without closing the main window. As of now, there are other ways to do that: JavaScript is able to send requests for server, so popups are rarely used. But sometimes they are still handy.</p>\n\n<p>In the past evil sites abused popups a lot. A bad page could open tons of popup windows with ads. So now most browsers try to block popups and protect the user.</p>\n\n<p>Most browsers block popups if they are called outside of user-triggered event handlers like onclick.</p>\n\n<p>If you think about it, that’s a bit tricky. If the code is directly in an onclick handler, then that’s easy. But what is the popup opens in setTimeout?</p>\n\n<p>Try this code:</p>\n\n<pre><code> // open after 3 seconds\nsetTimeout(() =&gt; window.open(\'http://google.com\'), 3000);\n</code></pre>\n\n<p>The popup opens in Chrome, but gets blocked in Firefox.</p>\n\n<p>…And this works in Firefox too:</p>\n\n<pre><code> // open after 1 seconds\nsetTimeout(() =&gt; window.open(\'http://google.com\'), 1000);\n</code></pre>\n\n<p>The difference is that Firefox treats a timeout of 2000ms or less are acceptable, but after it – removes the “trust”, assuming that now it’s “outside of the user action”. So the first one is blocked, and the second one is not.</p>\n\n<hr>\n\n<p>Original answer which was current 2012:</p>\n\n<blockquote>\n  <p>This solution for popup blocker checking has been tested in FF (v11),\n  Safari (v6), Chrome (v23.0.127.95) &amp; IE (v7 &amp; v9). Update the\n  displayError function to handle the error message as you see fit.</p>\n\n<pre><code>var popupBlockerChecker = {\n    check: function(popup_window){\n        var scope = this;\n        if (popup_window) {\n            if(/chrome/.test(navigator.userAgent.toLowerCase())){\n                setTimeout(function () {\n                    scope.is_popup_blocked(scope, popup_window);\n                },200);\n            }else{\n                popup_window.onload = function () {\n                    scope.is_popup_blocked(scope, popup_window);\n                };\n            }\n        } else {\n            scope.displayError();\n        }\n    },\n    is_popup_blocked: function(scope, popup_window){\n        if ((popup_window.innerHeight &gt; 0)==false){ \n            scope.displayError();\n        }\n    },\n    displayError: function(){\n       alert("Popup Blocker is enabled! Please add this site to your exception list.");\n    }\n};\n</code></pre>\n  \n  <p>Usage:</p>\n\n<pre><code>var popup = window.open("http://www.google.ca", \'_blank\');\npopupBlockerChecker.check(popup);\n</code></pre>\n</blockquote>\n\n<p>Hope this helps! :)</p>\n'}, {'answer_id': 27725432, 'author': 'DanielB', 'author_id': 4409047, 'author_profile': 'https://Stackoverflow.com/users/4409047', 'pm_score': 6, 'selected': False, 'text': '<p>I tried a number of the examples above, but I could not get them to work with Chrome. This simple approach seems to work with Chrome 39, Firefox 34, Safari 5.1.7, and IE 11. Here is the snippet of code from our JS library.</p>\n\n<pre><code>openPopUp: function(urlToOpen) {\n    var popup_window=window.open(urlToOpen,"myWindow","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=400, height=400");            \n    try {\n        popup_window.focus();   \n    } catch (e) {\n        alert("Pop-up Blocker is enabled! Please add this site to your exception list.");\n    }\n}\n</code></pre>\n'}, {'answer_id': 35663167, 'author': 'UncaAlby', 'author_id': 1411061, 'author_profile': 'https://Stackoverflow.com/users/1411061', 'pm_score': 4, 'selected': False, 'text': '<p>One "solution" that will <em>always</em> work regardless of browser company or version is to simply put a warning message on the screen, somewhere close to the control that will create a pop-up, that politely warns the user that the action requires a pop-up and to please enable them for the site.</p>\n\n<p>I know it\'s not fancy or anything, but it can\'t get any simpler and only requires about 5 minutes testing, then you can move on to other nightmares.</p>\n\n<p>Once the user has allowed pop-ups for your site, it would also be considerate if you don\'t overdo the pop-ups. The last thing you want to do is annoy your visitors.</p>\n'}, {'answer_id': 48521529, 'author': 'Michael Giovanni Pumo', 'author_id': 695749, 'author_profile': 'https://Stackoverflow.com/users/695749', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve tried lots of solutions, but the only one I could come up with that also worked with uBlock Origin, was by utilising a timeout to check the closed status of the popup.</p>\n\n<pre class="lang-js prettyprint-override"><code>function popup (url, width, height) {\n    const left = (window.screen.width / 2) - (width / 2)\n    const top = (window.screen.height / 2) - (height / 2)\n    let opener = window.open(url, \'\', `menubar=no, toolbar=no, status=no, resizable=yes, scrollbars=yes, width=${width},height=${height},top=${top},left=${left}`)\n\n    window.setTimeout(() =&gt; {\n        if (!opener || opener.closed || typeof opener.closed === \'undefined\') {\n            console.log(\'Not allowed...\') // Do something here.\n        }\n    }, 1000)\n}\n</code></pre>\n\n<p>Obviously this is a hack; like all solutions to this problem.</p>\n\n<p>You need to provide enough time in your setTimeout to account for the initial opening and closing, so it\'s never going to be thoroughly accurate. It will be a position of trial and error.</p>\n\n<p>Add this to your list of attempts.</p>\n'}, {'answer_id': 50587436, 'author': 'Yash Bora', 'author_id': 9194867, 'author_profile': 'https://Stackoverflow.com/users/9194867', 'pm_score': -1, 'selected': False, 'text': '<p>By using onbeforeunload event we can check as follows</p>\n\n<pre><code>    function popup()\n    {\n        var chk=false;\n        var win1=window.open();\n        win1.onbeforeunload=()=&gt;{\n            var win2=window.open();\n            win2.onbeforeunload=()=&gt;{\n                chk=true;\n            };\n        win2.close();\n        };\n        win1.close();\n        return chk;\n    }\n</code></pre>\n\n<p>it will open 2 black windows in background</p>\n\n<p>the function returns boolean value.</p>\n'}, {'answer_id': 54898902, 'author': 'wonsuc', 'author_id': 4729203, 'author_profile': 'https://Stackoverflow.com/users/4729203', 'pm_score': 1, 'selected': False, 'text': "<p>I combined @Kevin B and @DanielB's solutions.<br>\nThis is much simpler.</p>\n\n<pre><code>var isPopupBlockerActivated = function(popupWindow) {\n    if (popupWindow) {\n        if (/chrome/.test(navigator.userAgent.toLowerCase())) {\n            try {\n                popupWindow.focus();\n            } catch (e) {\n                return true;\n            }\n        } else {\n            popupWindow.onload = function() {\n                return (popupWindow.innerHeight &gt; 0) === false;\n            };\n        }\n    } else {\n        return true;\n    }\n    return false;\n};\n</code></pre>\n\n<p>Usage: </p>\n\n<pre><code>var popup = window.open('https://www.google.com', '_blank');\nif (isPopupBlockerActivated(popup)) {\n    // Do what you want.\n}\n</code></pre>\n"}, {'answer_id': 59999198, 'author': 'Lalit Umbarkar', 'author_id': 3983039, 'author_profile': 'https://Stackoverflow.com/users/3983039', 'pm_score': 1, 'selected': False, 'text': '<p>A simple approach <strong>if you own the child code</strong> as well, would be to create a simple variable in its html as below:</p>\n\n<pre class="lang-html prettyprint-override"><code>    &lt;script&gt;\n        var magicNumber = 49;\n    &lt;/script&gt;\n</code></pre>\n\n<p>And then check its existence from parent something similar to following:</p>\n\n<pre class="lang-js prettyprint-override"><code>    // Create the window with login URL.\n    let openedWindow = window.open(URL_HERE);\n\n    // Check this magic number after some time, if it exists then your window exists\n    setTimeout(() =&gt; {\n        if (openedWindow["magicNumber"] !== 32) {\n            console.error("Window open was blocked");\n        }\n    }, 1500);\n\n</code></pre>\n\n<p>We wait for some time to make sure that webpage has been loaded, and check its existence.\nObviously, if the window did not load after 1500ms then the variable would still be <code>undefined</code>. </p>\n'}, {'answer_id': 73744911, 'author': 'vladanPro', 'author_id': 617934, 'author_profile': 'https://Stackoverflow.com/users/617934', 'pm_score': 0, 'selected': False, 'text': '<p>For some popup blockers this don\'t work but i use it as basic solution and add try {} catch</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>try {\n        const newWindow = window.open(url, \'_blank\');\n        if (!newWindow || newWindow.closed || typeof newWindow.closed == \'undefined\')         {\n            return null;\n        }\n\n        (newWindow as Window).window.focus();\n        newWindow.addEventListener(\'load\', function () {\n            console.info(\'Please allow popups for this website\')\n        })\n        return newWindow;\n    } catch (e) {\n        return null;\n    }</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>This will try to call addEventListaner function but if popup is not open then it will break and go to catch, then ask if its object or null and run rest of code.</p>\n'}]
['https://Stackoverflow.com/questions/2914', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/434/']