new window doesn't open as tab in Chrome

爷,独闯天下 提交于 2019-12-19 04:18:11

问题


I'm building something ONLY for 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!

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?

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


回答1:


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 can do, 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);



回答2:


Chrome Add on "One Window": 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.



来源:https://stackoverflow.com/questions/9864747/new-window-doesnt-open-as-tab-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!