镀铬的window.open后Ajax请求就像弹出 [英] Chrome window.open after ajax request acts like popup
问题描述
我有一个情况,当用户按下一个按钮,我执行一个Ajax请求,然后使用Ajax请求的结果产生,我想在新标签页打开一个URL。然而,当我打电话的window.open在成功处理程序Ajax请求铬,它就像一个弹出一个新窗口中打开(和被阻止弹出阻滞剂)。我的猜测是,由于成功code是点击操作code的铬认为它不是由点击触发的,即使它是存在因果关系的点击同步。有没有什么办法prevent此未做Ajax请求同步?
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?
修改 下面是一些最起码的code,它演示了此行为:
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/
一注澄清:我比较conerned它打开在一个单独的窗口,而不是一个标签,比我这件事被阻止通过一个弹出式窗口拦截
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.
推荐答案
尝试添加
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();
编辑2
看起来你是保持同步的东西更好。
Edit 2
Looks like you're better of keeping things sync.
只要新的窗口是同根同源你有一些权力与JS操纵它。
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);
});
这篇关于镀铬的window.open后Ajax请求就像弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!