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']);
}
});
Written by Scott M
Related protips
1 Response
Thanks a lot for the explanation.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#