Javascript window.open() 在 AJAX 成功时的工作方式不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10223388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.open() works different on AJAX success
提问by T1000
Possible Duplicate:
window.open(url) different behavior - same code, different timing
It will be more easy for me to explain the problem if I just show you that example -> http://jsfiddle.net/RU2SM/
As you can see there are 2 buttons, one called'AJAX' and one called 'Direct'... So if you click on 'Direct' it opens window (new tab on Chrome) but if I try to make window.open() on AJAX success handler, it doesn't work same way.
I'm sure that the problem is from AJAX but I have no idea how to fix it.
Will appreciate any good ideas.
Thanks
如果我只向您展示该示例,我会更容易解释问题-> http://jsfiddle.net/RU2SM/
正如您所看到的,有 2 个按钮,一个称为“AJAX”,一个称为“Direct” ...因此,如果您单击“直接”,它会打开窗口(Chrome 上的新选项卡),但是如果我尝试在 AJAX 成功处理程序上创建 window.open(),则它不会以相同的方式工作。
我确定问题出在 AJAX 上,但我不知道如何解决。
将欣赏任何好主意。谢谢
回答by Rick Hoving
This works like a charm:
这就像一个魅力:
// Direct window.open()
$('#btnDirect').on('click',function(){
window.open('http://google.com')
})
var success = false; //NOTE THIS
// AJAX window.open()
$('#btnAJAX').on("click", function(){
$.ajax({
url: "/user/login/",
context: document.body,
async:false, //NOTE THIS
success: function(){ //THIS ALSO CHANGED
success = true
}
});
if(success){ //AND THIS CHANGED
window.open('http://google.com')
}
})
What this does is when the Ajax call is success it sets the variable success to true.
The async:false
propperty makes sure that the if statement is fired after the Ajax call is completed.
So the window.open is fired in the same circumstances as your direct link.
它的作用是当 Ajax 调用成功时,它会将变量 success 设置为 true。
该async:false
属性确保在 Ajax 调用完成后触发 if 语句。
因此 window.open 在与您的直接链接相同的情况下被触发。
回答by Gareth
The issue is that browsers will often block window.open
s unless they're called in direct response to a user action. That's why your click handler works (a click is a user action) but your AJAX handler doesn't.
问题是浏览器通常会阻止window.open
s,除非它们被调用以直接响应用户操作。这就是为什么您的点击处理程序有效(点击是用户操作)但您的 AJAX 处理程序无效。
One solution is to open the window during the initial click action, then update its location on AJAX success (or close it again on AJAX failure).
一种解决方案是在初始单击操作期间打开窗口,然后在 AJAX 成功时更新其位置(或在 AJAX 失败时再次关闭它)。
Otherwise you'll have to get the user to explicitly allow popups from your domain in their browser.
否则,您必须让用户在其浏览器中明确允许来自您域的弹出窗口。
回答by zhyder
Better way implement any logic after success of ajax call, there is an event fired on every ajax call execution i.e. $.ajax.Request.doneand $.ajax.Request.fail. $.ajax.Request.done(function(){ if(success){ // Implement logic } });
在 ajax 调用成功后实现任何逻辑的更好方法,每个 ajax 调用执行都会触发一个事件,即$.ajax.Request.done和$.ajax.Request.fail。 $.ajax.Request.done(function(){ if(success){ // 实现逻辑 } });
回答by Tobias Krogh
as an addition it is also worth mentioning that using async: false and then calling window.open works in chrome and firefox but can cause trouble in safari... it doesn't even give an info that a popup was blocked
另外值得一提的是,使用 async: false 然后调用 window.open 可以在 chrome 和 firefox 中工作,但可能会导致 safari 出现问题......它甚至没有提供弹出窗口被阻止的信息