javascript 新窗口不会在 Chrome 中作为标签打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9864747/
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
new window doesn't open as tab in Chrome
提问by eric.itzhak
I'm building something ONLYfor Chrome.
我建立的东西只适用于Chrome。
I want to open several tabs with window.open
(which Chrome blocks but I can live with enabling it).
But Chrome opens them as new windows instead of tabs!
我想打开几个选项卡window.open
(Chrome 阻止但我可以启用它)。但是 Chrome 会将它们作为新窗口而不是标签打开!
And for some unclear reason I found only information regarding the opposite.How can I achive this?
由于某些不清楚的原因,我只找到了关于相反的信息。我怎样才能做到这一点?
And if at it, how can I invoke programmatic tab openings without Chrome blocking them?
如果这样做,如何在不让 Chrome 阻止的情况下调用程序化选项卡打开?
EDIT:I have seen some posts saying it's not possible and that's it's a browser preference. First of all, I have no idea how to set that preference! Secondly, I saw people claiming they did it, so who to believe to?
编辑:我看到一些帖子说这是不可能的,这是浏览器的偏好。首先,我不知道如何设置该偏好!其次,我看到有人声称他们做到了,那么该相信谁呢?
EDIT 2:I found out that Chrome opens new windows and not open tabs because it's a JavaScript window opening and not user clicks. Anyone knows how I can fake a real click? Because calling the click event still counts as not user clicks
编辑 2:我发现 Chrome 打开新窗口而不是打开选项卡,因为它是一个 JavaScript 窗口打开而不是用户点击。有谁知道我如何伪造真正的点击?因为调用 click 事件仍然算作不是用户点击
回答by Dan Herbert
If your popup is created by a direct user action (not blocked by the popup blocker) and using the default options it will open in a new tab. If you create it programmatically, it will open as a new window. There is no way to change this behavior.
如果您的弹出窗口是由直接用户操作(未被弹出窗口阻止程序阻止)创建的,并且使用默认选项,它将在新选项卡中打开。如果您以编程方式创建它,它将作为一个新窗口打开。没有办法改变这种行为。
What you cando, although it is a really bad hack, is create the popup on a user action and then set the location to the final destination using a reference to the popup later on, like the following:
尽管这是一个非常糟糕的黑客攻击,但您可以做的是在用户操作上创建弹出窗口,然后稍后使用对弹出窗口的引用将位置设置为最终目的地,如下所示:
<a href="javascript:;" id="testAnchor" />
var popup1 = null;
document.getElementById('testAnchor').onclick = function() {
popup1 = window.open('about:blank', 'popup1');
}
setTimeout(function() {
popup1.location = 'LOCATION_ON_THE_SAME_ORIGIN_AS_THE_OPENER';
}, 5000);
回答by Martin
Chrome Add on "One Window": https://chrome.google.com/webstore/detail/one-window/papnlnnbddhckngcblfljaelgceffobn
Chrome 添加“一个窗口”:https: //chrome.google.com/webstore/detail/one-window/papnlnnbddhckngcblfljaelgceffobn
Automatically moves new Windows and Popups as Tab to the Main-Window. So it is never more than a single Window open.
自动将新窗口和弹出窗口作为选项卡移动到主窗口。所以它永远只是一个打开的窗口。