javascript window.open with target '_blank' 打开一个新的浏览器窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24282604/
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 with target '_blank' opens a new browser window
提问by nrsharma
I am trying to open an link in new browser tab (not in new window).
我正在尝试在新的浏览器选项卡中(不在新窗口中)打开一个链接。
When I place an link on page like
当我在页面上放置一个链接时
<a href="#" onclick="window.open('http://www.google.com', '_blank');"> Click Here</a>
when user click on link it will open google in new browser tab. That's fine
当用户点击链接时,它将在新的浏览器标签中打开谷歌。没关系
BUT
但
$.ajax({
type: "POST",
url: someURL,
data: {somedata},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
window.open('http://www.google.com', '_blank'); // it's always open in new browser window and I want a new tab
},
failure: function (response) {
alert(response);
return;
}
});
When I try this within an AJAX web method, it always opens it in new browser window; but I want to open it in new tab as it's working for above example.
当我在 AJAX 网络方法中尝试这个时,它总是在新的浏览器窗口中打开它;但我想在新选项卡中打开它,因为它适用于上述示例。
I understand this is browser specific functionality but somehow I need to accomplish this.
我知道这是浏览器特定的功能,但我需要以某种方式完成此操作。
回答by Clindo
try this
试试这个
onclick="window.open('http://www.google.com', '_self');
回答by A.T.
well it is browser specific, i tested it on mozilla and is working fine, but on chrome it open in new browser window. You can suggest to chrome makers or call ajax synchronus.
好吧,它是特定于浏览器的,我在 mozilla 上对其进行了测试并且工作正常,但是在 chrome 上它在新的浏览器窗口中打开。您可以建议 chrome 制造商或调用 ajax 同步。
use async:false
will work.
使用async:false
将工作。
NOTE:In IE, open new browser window is default behaviour. user need to set settings explicitly
注意:在 IE 中,打开新的浏览器窗口是默认行为。用户需要明确设置
回答by Sandeeproop
You have to play a little trick here. You have to create a hidden a link tag with target='_blank' and set the href of this link tag on ajax success and then trigger the click of this link tag for eg.
你必须在这里玩个小把戏。您必须使用 target='_blank' 创建一个隐藏的链接标签,并在 ajax 成功时设置此链接标签的 href,然后触发此链接标签的点击,例如。
HTML code
HTML代码
<a href="#" id="hidden_link" target="_blank">hidden</a>
Js code
js代码
$.ajax({
type: "POST",
url: someURL,
data: {somedata},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var hiddenLink = $("hidden_link");
hiddenLink.attr("href",response.href);
hiddenLink[0].click();
},
failure: function (response) {
alert(response);
return;
}
});
Here is the working fiddlefor above code
这是上述代码的工作小提琴