Created
September 5, 2014 00:53
-
-
Save miguelmota/a3ad17e9e0fdc0a00cbd to your computer and use it in GitHub Desktop.
Open window in new tab, avoiding popup blocker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var link = document.getElementById('link'); | |
function open() { | |
var w = window.open('', 'MyWindow'); | |
// Ajax call | |
setTimeout(function() { | |
w.location.href = 'http://example.com'; | |
}, 100); | |
} | |
link.addEventListener('click', open); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work because the
location.href
is set inside a callback function forsetTimeout()
. Even though the timer is triggered by the user, it shouldn't work. The browser sees this as an automated timer triggering the redirection. Thelocation.href
must be inside a user-driven event callback for a "popup" to be allowed.