Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My Modified Popup Blocker Checker/Handler Code (JS)
// Based on Stackoverflow answer here: http://stackoverflow.com/a/13652769/7656
// My modified handler
MyGlobalScope.popupBlockerChecker = {
check: function(popup_window, urlAttemptingToOpen){
var _scope = this;
if (popup_window) {
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function () {
_scope._is_popup_blocked(_scope, popup_window, urlAttemptingToOpen);
},200);
}else{
popup_window.onload = function () {
_scope._is_popup_blocked(_scope, popup_window, urlAttemptingToOpen);
};
}
}else{
_scope._displayError(urlAttemptingToOpen);
}
},
_is_popup_blocked: function (scope, popup_window, urlAttemptingToOpen) {
if ((popup_window.innerHeight > 0) == false) { scope._displayError(urlAttemptingToOpen); }
},
_displayError: function (urlAttemptingToOpen) {
// NOTE external dependency here of toastr. But you can notify the user however you want.
toastr.error("Popup blocker is enabled! Please add this site to your exception list or turn off the blocker. " +
"<a target='_blank' style='color: blue;' href='" + urlAttemptingToOpen + "'>Click here</a> to open the window.");
}
};
// Then, anywhere in code, I invoke like this:
var urlToOpen = '/#/my_url_with_parameters';
var popup = window.open(urlToOpen, 'MyWindowName', {});
MyGlobalScope.popupBlockerChecker.check(popup, urlToOpen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.