使用JavaScript的window.open不能正常工作 [英] Javascript window.open not working

查看:354
本文介绍了使用JavaScript的window.open不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定。我试图登录到Twitter。该窗口没有打开这个code。这被惊动了响应不为空,是一个链接到一个登录界面。任何想法?

  VAR URL =./twitter_login.php;
VAR CON = createPH prequest();

con.open(POST,网址,真实);
con.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
con.send();

变种响应=;

con.onreadystatechange =功能(){

    如果(con.readyState == 4和&安培; con.status == 200){

        响应= con.responseText;
        警报(响应);
        的window.open(回应,推特,菜单栏= 1,可调整大小= 1,宽= 350,高度= 500);

    }

}
 

解决方案

包含在大多数浏览器标准的弹出窗口阻止程序逻辑,这些天将阻止任何调用的window.open()不属于用户操作的直接结果。由定时器或任何异步回调(如您的AJAX功能齐全)触发code将被视为不是直接由用户操作引起的,在新弹出的窗口中一般会被阻止。

您可以验证这就是正在发生的事情通过临时改变浏览器的弹出窗口阻止(将其关闭),并看到它,然后开始工作。

可能是你需要做的是一个解决办法是建立在那开始的code这个线程,然后把内容放到窗口,当你得到你的Ajax响应用户操作窗口。浏览器可能会允许的。我知道这是从视觉角度不太理想,但你可以把窗口中的一些临时的内容,直到Ajax响应进来(像载入中...)。

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");        

    }

}

解决方案

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...").

这篇关于使用JavaScript的window.open不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆