8

I have a small bit of javascript intended to open two or more tabs. This works fine in FF and IE, but chrome opens the second one in a new window instead of tab. It isn't dependant on the url as I've tried it with two identical url's. First opens in tab, second one in new window.

Here's my code snippet:

for(var i=0 ; i<sites.length ;i++)
{
    window.open(sites[i].Url);
}
Bagelzone Ha'bonè
  • 1,182
  • 1
  • 13
  • 28
user1151653
  • 83
  • 1
  • 1
  • 4

2 Answers2

12

Chrome automatically opens a URL in a new tab only if it's user generated action, limited to one tab per user action. In any other case, the URL will be opened in a new window (which, BTW, is blocked by default on Chrome).
window.open must be called within a callback which is triggered by a user action (e.g. onclick) for the page to open in a new tab instead of a window.

In your example, you attempt to open N tabs upon user action. But only the first one is opened in a new tab (because it's a user generated action). Following that, any other URL will be opened in a new window.

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

Community
  • 1
  • 1
Bagelzone Ha'bonè
  • 1,182
  • 1
  • 13
  • 28
  • 1
    Thanks, I was afraid it would be something like this. I did report it to google as a bug as I feel that a function should behave consistent, and not change behavior the second time it's called. Besides that, other browsers simply open tabs. – user1151653 May 26 '13 at 14:04
  • 1
    I believe it's by design, since new windows have a block mechanism and new tabs doesn't. It's the same story as with alerts. If you have several consecutive alerts (in the same scope/context) - the first one will be shown normally, and all the following alerts would have a "prevent further dialogs" checkbox. – Bagelzone Ha'bonè May 26 '13 at 15:58
  • 1
    I understand the considerations, but frankly I think it's a silly design if it leads to unexpected behavior. I like to know what a call to a function does, and if one call to window.open leads to completely different results from another call, then the design is in error. Besides that both FF and IE have pop-up blockers and implement this correctly. – user1151653 May 26 '13 at 19:38
0

I came across this question What is the (function() { } )() construct in JavaScript? which gives explanation on IIFE. I think this can be used over. Please bear with me I don't have deep knowledge about javascript. But I tried as below and its working.

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.

Ganesh G
  • 41
  • 1
  • 1
  • 5
  • Doesn't work in Chrome version 49.0.2623.112 (at least not as bookmarklet): Google opens, Yahoo gets blocked – Florian Straub Apr 07 '18 at 23:40
  • I tried on Chrome Version 65.0.3325.181 (Official Build) (64-bit) it works. You have to enable pop up which is by default blocked. For ex. in index html in which if you have included the code you need to enable pop up when you open the index html in chrome. – Ganesh G Apr 11 '18 at 10:04
  • you need to reload the page once you have enabled the pop up – Ganesh G Apr 11 '18 at 10:17
  • Thanks for the research. Meanwhile I reduced my bookmarklet to collect the links and write them in a textarea. From there I copy them to Chrome extension "Bulk URL opener", which does the rest for me. If this solution gets on my nerves one day, I will try again. – Florian Straub Apr 14 '18 at 10:29