JavaScript 在新标签页中打开,而不是 Chrome 浏览器中的窗口

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

JavaScript open in a new tab, not window in Chrome browser

javascriptgoogle-chrometabs

提问by JEYASHRI R

I am using,

我在用,

window.open("Download.php");

This file opens in a new tab for all browsers. But in Chrome, it opens in a new window. So I have tried:

此文件将在所有浏览器的新选项卡中打开。但在 Chrome 中,它会在新窗口中打开。所以我试过:

window.open("Download.php",'_blank'); 
window.open("Download.php",'_new');

But it still opens in new window, not a tab. I don't know if this is a coding problem or default settings problem. I am using Chrome v31.0.1650.57 m.

但它仍然在新窗口中打开,而不是选项卡。我不知道这是编码问题还是默认设置问题。我正在使用 Chrome v31.0.1650.57 m。

回答by Zaheer Ahmed

You can try:

你可以试试:

var tab=window.open("Download.php",'_blank'); 
tab.focus();

Steps to verify

验证步骤

create a html file with markup:

创建一个带有标记的 html 文件:

<html>
 <body>
   <script>
     function newTab(){
        var tab=window.open("http://conceptf1.blogspot.com/2013/11/javascript-closures.html",'_blank'); 
        tab.focus();
     }
   </script>
   <a href="#" onclick="newTab()">Open Tab</a>
  </body>
</html>

Open it in chrome and click the link.

在 chrome 中打开它并单击链接。

回答by The Spooniest

Short answer: you can't. At least for now, the browser vendors view this as something that should strictly be left to users to define for themselves. You set your target to something that should be a new window, and the browser will use the user's preferences to decide whether it opens as a new window or a new tab.

简短的回答:你不能。至少目前,浏览器供应商认为这应该严格留给用户自己定义。您将目标设置为应该是新窗口的内容,浏览器将使用用户的首选项来决定它是作为新窗口还是新选项卡打开。

Long answer: There's a CSS property that would do this on the HTML side, but none of the browsers support it. According to the proposal, you'd use target-new: tabfor links that you explicitly want to open in a new tab, and target-new: windowfor those you want to open in a new window. I don't know of any DOM proposals to do something analogous to this on the JavaScript side.

长答案:有一个 CSS 属性可以在 HTML 端执行此操作,但没有一个浏览器支持它。根据提案,您将target-new: tab用于明确希望在新选项卡中打开的链接,以及target-new: window希望在新窗口中打开的链接。我不知道有任何 DOM 建议在 JavaScript 方面做类似的事情。