Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript window.open not working

Ok. I'm trying to login to twitter. The window is not opening in this code. The response that gets alerted is not null and is a link to a login screen. Any ideas?

var url = "./twitter_login.php"; var con = createPHPRequest();  con.open("POST",url,true); con.setRequestHeader("Content-type","application/x-www-form-urlencoded"); con.send("");  var response = "";  con.onreadystatechange = function() {      if(con.readyState==4 && con.status==200) {          response = con.responseText;             alert(response);         window.open(response,"twitter","menubar=1,resizable=1,width=350,height=500");              }  } 
like image 789
Quinn Finney Avatar asked Aug 05 '12 23:08

Quinn Finney


People also ask

How can we open a window in JavaScript?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.

How do you call a popup in JavaScript?

The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.

How do I open a JavaScript browser?

In JavaScript, window.open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.


2 Answers

The standard popup-blocker logic contained in most browsers these days will block any calls to window.open() that are not the direct result of a user action. Code that is triggered by timers or by any asynchronous callback (like your ajax ready function) will be treated as NOT caused directly by user actions and the new popup window will generally be blocked.

You can verify this is what is happening by temporarily changing your browser's popup blocking (turning it off) and see that it then starts working.

Probably what you need to do as a work-around is to create the window upon the user action that started this thread of code and then put the content into the window when you get your ajax response. The browser will probably allow that. I know that's less desirable from a visual perspective, but you can put some temporary content in the window until the ajax response comes in (something like "loading...").

like image 81
jfriend00 Avatar answered Sep 21 '22 22:09

jfriend00


Just had this exact same issue. Just in case you wanted the code that fixed it. I used this:

newWindow = window.open("", "_blank");  request = $.ajax({ ... my request which returns a url to load ... })  request.done((function(_this) {         return function(data, textStatus, jqXHR) {           return newWindow.location = data.Url;         };       })(this)); 
like image 33
t_warsop Avatar answered Sep 21 '22 22:09

t_warsop