Window.open behavior in chrome tabs / windows

I have a small javascript bit designed to open two or more tabs. This works fine in FF and IE, but chrome opens the second in a new window instead of a tab. It is URL independent, as I tried it with two identical URLs. First, a tab opens, the second in a new window.

Here is my code snippet:

for(var i=0 ; i<sites.length ;i++) { window.open(sites[i].Url); } 
+8
javascript google-chrome
source share
2 answers

Chrome automatically opens the URL in a new tab only if it is a user action limited to one tab for each user action. In any other case, the URL will be opened in a new window (which, by the way, is blocked by default in Chrome).
window.open must be called in a callback that is triggered by a user action (for example, onclick) to open the page in a new tab instead of a window.

In your example, you are trying to open N tabs with user action. But only the first one is open in a new tab (because this is an action created by the user). After that, any other URL will open in a new window.

Similar question: force window.open () create a new tab in chrome (see maclema answer)

+11
source share

I came across this question What is the function (function () {}) () in JavaScript? , which gives explanations for IIFE. I think it can be used. Please carry me. I have no deep knowledge of javascript. But I tried, as shown below, and its work.

 var sites = [{"url" : "http://www.google.com"} , {"url" : "http://www.yahoo.com"} , {"url" : "http://www.msn.com"}]; console.log(sites); for( var i=0 ; i < sites.length ;i++) { (function(i) { console.log(i); window.open(sites[i].url , "_blank"); })(i); } 

It opens the url in new tabs in chrome.

0
source share

All Articles