javascript 在 chrome 的新标签页中打开一个新链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10024527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:30:13  来源:igfitidea点击:

Open a new link in new tab in chrome

javascriptjqueryhtml

提问by 1509

I am trying to open a link in a new tab on ajax success, but the link opens in a new window. How to forcefully open the link in a tab rather than in a new window in chrome with jquery or javascript.

我试图在 ajax 成功的新选项卡中打开一个链接,但该链接在新窗口中打开。如何使用 jquery 或 javascript 在 chrome 中的选项卡中而不是在新窗口中强制打开链接。

Code:

代码:

$("#A1").click(function (event) {
    $.ajax({ 
        type: "POST", 
        url: "Home.aspx/btnTestProject", 
        data: "{'preview':'" + inventory + "' }", 
        contentType: "application/json; charset=utf-8", 
        datatype: "json", 
        cache: false, 
        success: function (response) { 
            window.open("TestnRun.aspx"); //opens in new window 
        }    
    }); 

    window.open("TestnRun.aspx"); //opens in new tab 
});

回答by ZeNo

In general you cannot control it. As this is user preference to open link with target="_blank" in new window or tab.

一般来说,你无法控制它。因为这是用户偏好在新窗口或选项卡中打开带有 target="_blank" 的链接。

When browsers starts supporting css-3 completely then you will be having an option in future:

当浏览器开始完全支持 css-3 时,您将来会有一个选择:

http://www.w3.org/TR/2004/WD-css3-hyperlinks-20040224/#target-new

http://www.w3.org/TR/2004/WD-css3-hyperlinks-20040224/#target-new

You can use this:

你可以使用这个:

#anchorId { target-new: tab ! important }

回答by Davide Dolla

I saw the same thing.

我看到了同样的事情。

And I've noted that (I'm using jquery.1.9.1):

我注意到(我使用的是 jquery.1.9.1):

if you call $.ajax in async, or % the browser (Chrome) open in NEW WINDOW, else if you run a JSON call SYNCthe window will be open tabbed!

如果您异步调用 $.ajax,或者在新窗口中打开浏览器 (Chrome) %,否则如果您运行 JSON 调用SYNC,窗口将打开选项卡

This open in a NEW WINDOW, pop-up:

这在新窗口中打开,弹出:

  $.ajax({ url: "/cliFindExist/", 
    async: true, 
    function(data){... 
    window.open("some.aspx","_blank")});

This open window TABBED

这个打开的窗口 TABBED

$.ajax({ url: "/cliFindExist/", 
async: false, // <<<<<<<<<<<<< NOTE
function(data){... 
window.open("some.aspx","_blank")});

JQuery may be change the way/context when it call the function? depending if it run a sync or an async call.

JQuery 在调用函数时可能会改变方式/上下文?取决于它是运行同步还是异步调用。

回答by dash1e

Use target "_blank" like this

像这样使用目标“_blank”

<a href="http://www.google.it" target="_blank">My link</a>

or with Javascript like this

或者像这样使用Javascript

<button onclick="window.open('http://www.google.it','_blank')">Button</button>