Thayne Thayne - 9 days ago 5x
Ajax Question

Chrome after ajax request acts like popup

I have a situation where, when a user pushes a button I perform an ajax request, and then use the result of the ajax request to generate a URL which I want to open in a new tab. However, in chrome when I call in the success handler for the ajax request, it opens in a new window like a popup (and is blocked by popup-blockers). My guess is that since the the success code is asynchronous from the click handling code that chrome thinks it wasn't triggered by a click, even though it is causally related to a click. Is there any way to prevent this without making the ajax request synchronous?

Here is some minimal code that demonstrates this behaviour:

$('#myButton').click(function() {
type: 'POST',
url: '/echo/json/',
data: {'json': JSON.stringify({
success: function(data) {,'_blank');

One note of clarification: I am more conerned about it opening in a separate window rather than a tab, than I am about it being blocked by a popup blocker.


Try to add,'_blank');


Well, I don't think you can get around popup-blockers when opening a page that's not the immediate result of a user action (i.e. not async).

You could try something like this though, it should look like a user action to a popup-blocker:

var $a = $('<a>', {
        href: url,
        target: '_blank' 


Edit 2

Looks like you're better of keeping things sync.

As long as the new window is "same origin" you have some power to manipulate it with JS.

$('#a').on('click', function(e){
    var wi ='about:blank', '_blank');

    setTimeout(function(){ // async
        wi.location.href = '';
    }, 500);