Hi,
When i started looking out for javascript code that can detect Chrome’s (17.0) popup blocker, I tried lots of snippets. But most of them dint really work. So once i got it working, I thought it would be good to share with you so that you can make use of the same in your projects. The following is the code snippet::
1 2 3 4 5 6 7 8 9 |
var pop = window.open("about:blank", "new_window_123", "height=150,width=150"); // Detect pop blocker setTimeout(function() { if(!pop || pop.closed || pop.closed == "undefined" || pop == "undefined" || parseInt(pop.innerWidth) == 0){ pop && pop.close(); alert("Popups must be enabled."); }else{ pop && pop.close(); }}, 1000); |
What this script does is,
1. Tries to open a popup window and store the window handler in pop variable.
2. Declare an anonymous function with setTimeout set to 1000 milli sec, i.e. 1 sec.
3. So apparently once the 1 sec is complete and the anonymous function gets executed,
3.1 We check if the popup has been loaded, we can see that the ‘pop.innerWidth’ is being used to detect the chrome’s popup mechanism as the popup window’s innerWidth property would have been set to 0 if the popup blocker is enabled.
4. Once all is done we close the popup we had opened.
Send me your comments/suggestions to further optimize the solution.
P.S : as per updates from Anders the script was not working on Chrome 19, now with the inputs from Anders i have modified the script to check for :
1 2 3 |
// START pop.document.documentElement.clientWidth != 150 || pop.document.documentElement.clientHeight != 150 // END |
This compares the height and width values to the exact height and width values I set on window.open() method i.e. 150X150 , this enables us to correctly find if the popup is enabled or not. I have checked it on Chrome 21.
Please check out the below mentioned updated code and let me know if you face any issues.
Cheers.
Anand
1 2 3 4 5 6 7 8 9 10 |
var pop = window.open("about:blank", "new_window_123", "height=150,width=150"); // Detect pop blocker setTimeout(function() { if(!pop || pop.closed || pop.closed == "undefined" || pop == "undefined" || parseInt(pop.innerWidth) == 0 || pop.document.documentElement.clientWidth != 150 || pop.document.documentElement.clientHeight != 150){ pop && pop.close(); alert("Popups must be enabled."); }else{ alert("Popups is enabled."); pop && pop.close(); }}, 1000); |
This doesn’t work anymore for me (Chrome version 19). I resorted to check if the opened windows size (pop.document.documentElement.clientHeight and pop.document.documentElement.clientWidth) was the same as the size I requested in window.open().
I have updated the post now with changes that work in Chrome 21. Please leave your comments.
Cheers.
Thank you for your comments Anders,
I shall check your solution and update the post 🙂
hye, thank for the code..however it works only in chrome..not in firefox..in firefox, i only get ‘Popup Must Be Enabled’ even when i already disable the blocker..
please advice..thank you!
Hi Phizy,
Can you give me the firefox version you was testing on ??
Cheers
Anand
I’m testing it with the latest version of Firefox on Windows 7 and I get the same result.
I eliminated the part of the script that checks for height and now it works on the latest version of Firefox too.
Correction, it works when the window is minimized but not when it is maximized.