I've been working on an issue with my program executing too quickly to detect whether popup is being blocked by the browser. Even with the timeout, sometimes the window that is opened doesn't get closed. I've also tried opening a window and passing a value back to the parent window and that didn't work either.
setTimeout(DetectPopUp(), 1000);
function DetectPopUp() {
var puTest = setTimeout(window.open("", "", "width=1,height=1,top=2000"));
try {
puTest.close();
return false;
} catch (e) {
return true;
}
}
Any ideas on how to do this?
As already mentioned the source of the OP's currently failing code is how the OP uses
setTimeoutand its return value. The latter is not the handle of awindowreference as the OP seems to assume but the timeout specific identifier which can be used for e.g. canceling the timeout before the execution of the to be delayed code happens.Thus, the OP actually does not need to write a deferred version of a popup blocker test since
window.openimmediately returns the result of its action which either is aWindowobject (no popup blocker) or is thenullvalue (most probably due to a popup blocker).But of cause one could come up with promise based test functionality implemented as a single async function which then can be utilized either as chained promise or within another async function by the
awaitoperator.