Skip to content

Instantly share code, notes, and snippets.

@tylerforsythe
Created September 29, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerforsythe/452ceaad62f507d7cb7bd7ddbffe650c to your computer and use it in GitHub Desktop.
Save tylerforsythe/452ceaad62f507d7cb7bd7ddbffe650c to your computer and use it in GitHub Desktop.
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