Avoid browser popup blockers
Based on Jason Sebring's very useful tip, and on the stuff covered here and there, I found a perfect solution for my case:
Pseudo code with Javascript snippets:
immediately create a blank popup on user action
var importantStuff = window.open('', '_blank');
(Enrich the call to
window.open
with whatever additional options you need.)Optional: add some "waiting" info message. Examples:
a) An external HTML page: replace the above line with
var importantStuff = window.open('http://example.com/waiting.html', '_blank');
b) Text: add the following line below the above one:
importantStuff.document.write('Loading preview...');
fill it with content when ready (when the AJAX call is returned, for instance)
importantStuff.location.href = 'https://example.com/finally.html';
Alternatively, you could close the window here if you don't need it after all (
if ajax request fails
, for example - thanks to @Goose for the comment):importantStuff.close();
I actually use this solution for a mailto redirection, and it works on all my browsers (windows 7, Android). The _blank
bit helps for the mailto redirection to work on mobile, btw.
The general rule is that popup blockers will engage if window.open
or similar is invoked from javascript that is not invoked by direct user action. That is, you can call window.open
in response to a button click without getting hit by the popup blocker, but if you put the same code in a timer event it will be blocked. Depth of call chain is also a factor - some older browsers only look at the immediate caller, newer browsers can backtrack a little to see if the caller's caller was a mouse click etc. Keep it as shallow as you can to avoid the popup blockers.