zn6bgw
Last Updated: February 25, 2016
·
9.656K
· stereoscott
Fe9d2067fd141c6552e154f68a091832

Prevent popup blocker in jquery ajax callback

Let's say you want a link to make an ajax request to your server to fetch a URL and then you'd like to open that URL in a new window. You first guess might be to do this:

$('.external-link').on('click', function(e) {
  $.ajax({
    url: $(e.currentTarget).attr('href'),
    dataType: 'json',
    success: function(r) {
      window.open(response['url']);
    }
});

However, most browsers will block any popups triggered while in "script execution context". To get around this, update your $.ajax call to run synchronously.

$('.external-link').on('click', function(e) {
  $.ajax({
    url: $(e.currentTarget).attr('href'),
    dataType: 'json',
    async: false,
    success: function(r) {
      window.open(response['url']);
    }
});
Say Thanks
Respond