Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created September 5, 2014 00:53
Show Gist options
  • Save miguelmota/a3ad17e9e0fdc0a00cbd to your computer and use it in GitHub Desktop.
Save miguelmota/a3ad17e9e0fdc0a00cbd to your computer and use it in GitHub Desktop.
Open window in new tab, avoiding popup blocker
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);
@tyrumus
Copy link

tyrumus commented Jun 2, 2017

This doesn't work because the location.href is set inside a callback function for setTimeout(). Even though the timer is triggered by the user, it shouldn't work. The browser sees this as an automated timer triggering the redirection. The location.href must be inside a user-driven event callback for a "popup" to be allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment