Javascript window.open 不起作用 [英] Javascript window.open not working

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

问题描述

好的.我正在尝试登录推特.此代码未打开窗口.收到警报的响应不为空,并且是登录屏幕的链接.有什么想法吗?

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

    }

}

推荐答案

如今大多数浏览器中包含的标准弹出窗口阻止程序逻辑将阻止对 window.open() 的任何调用,这些调用不是用户操作的直接结果.由计时器或任何异步回调(如您的 ajax 就绪函数)触发的代码将被视为不是由用户操作直接引起的,并且新的弹出窗口通常会被阻止.

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.

作为变通方法,您可能需要根据启动此代码线程的用户操作创建窗口,然后在收到 ajax 响应时将内容放入窗口.浏览器可能会允许这样做.我知道从视觉角度来看这不太理想,但是您可以在窗口中放置一些临时内容,直到 ajax 响应出现(类似于正在加载...").

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天全站免登陆