Last Updated: November 15, 2018
·
18.77K
· stereoscott

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']);
    }
});

1 Response
Add your response

Thanks a lot for the explanation.

over 1 year ago ·