Processing
 

Chrome window.open after ajax request acts like popup

25/02/2014 07:39#1

Thayne

Member

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

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 window.open 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?

EDIT Here is some minimal code that demonstrates this behaviour:

$('#myButton').click(function() {     $.ajax({         type: 'POST',         url: '/echo/json/',         data: {'json': JSON.stringify({             url:'http://google.com'})},         success: function(data) {             window.open(data.url,'_blank');         }     }); }); 

http://jsfiddle.net/ESMUA/2/

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.

25/02/2014 07:54Top#2

pstenstrm

Member

Joined at: 11 months ago

Post: 9

Thank: 0

Thanked: 0

Try to add

window.open(url,'_blank'); 

Edit

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'      });  $(document.body).append($a); $a.click(); 

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){     e.preventDefault();     var wi = window.open('about:blank', '_blank');      setTimeout(function(){ // async         wi.location.href = 'http://google.com';     }, 500); }); 
15/05/2014 22:17Top#3

rajesh

Member

Joined at: 11 months ago

Post: 42

Thank: 0

Thanked: 0

Try adding async: false. It should be working

$('#myButton').click(function() { $.ajax({     type: 'POST',     async: false,     url: '/echo/json/',     data: {'json': JSON.stringify({         url:'http://google.com'})},     success: function(data) {         window.open(data.url,'_blank');     } }); }); 
03/11/2015 15:59Top#4

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

The answer posted by @pstenstrm above (Edit 2) mostly works, but I added just one line to it to make the solution more elegant. The ajax call in my case was taking more than a second and the user facing a blank page posed a problem. The good thing is that there is a way to put HTML content in the new window that we've just created.

e.g:

$('#a').on('click', function(e){     e.preventDefault();     var wi = window.open('about:blank', '_blank');     $(wi.document.body).html("<p>Please wait while you are being redirected...</p>");      setTimeout(function(){ // async         wi.location.href = 'http://google.com';     }, 500); }); 

This fills the new tab with the text "Please wait while you are being redirected..." which seems more elegant than the user looking at a blank page for a second. I wanted to post this as the comment but don't have enough reputation.

28/05/2014 14:00Top#5

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

What worked for me was:

var win = window.open('about:blank', '_blank');  myrepository.postmethod('myserviceurl', myArgs) .then(function(result) {     win.location.href = 'http://yourtargetlocation.com/dir/page'; }); 

You open the new window tab before the sync call while you're still in scope, grab the window handle, and then re-navigate once you receive the ajax results in the promise.

Similar articles

Chrome, THREE.js: Cross-origin image load denied

5 years ago - Reply: 5 - Views: 269

Prevent Google Chrome Log XMLHttpRequest

3 years ago - Reply: 5 - Views: 276

How can I include ChromeDriver in a JAR?

3 years ago - Reply: 3 - Views: 81

Chrome geolocation not working on any site

3 years ago - Reply: 1 - Views: 46

wrong borders' width in android browser

3 years ago - Reply: 4 - Views: 206

Uncaught SyntaxError: Unexpected token < On Chrome

3 years ago - Reply: 10 - Views: 468

Flashdata not getting cleared in Codeigniter

3 years ago - Reply: 5 - Views: 57

Illegal access Javascript error in Chrome

3 years ago - Reply: 1 - Views: 51

ng-repeat not updating in Chrome

3 years ago - Reply: 1 - Views: 81

Applying border to a checkbox in Chrome

4 years ago - Reply: 2 - Views: 118

open browser plugin using access vba

3 years ago - Reply: 1 - Views: 25

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re