Open multiple links in Chrome at once as new tabs

70,760

Solution 1

You can do this in vanilla JavaScript:

<html>
<head>
<script type="text/javascript">
function open_win() {
    window.open("http://www.java2s.com/")
    window.open("http://www.java2s.com/")
}
</script>
</head>

<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>

</html>

Here is a more Chrome-specific implementation (if popup blockers are giving you difficulty):

var linkArray = []; // your links
for (var i = 0; i < linkArray.length; i++) {
    // will open each link in the current window
    chrome.tabs.create({
        url: linkArray[i]
    });
}

Here is some documentation: https://developer.chrome.com/extensions/tabs

Solution 2

The reason that the browser extension can do it is because Chrome extensions have access to a special Chrome API, which lets you use:

chrome.windows.create({tabid: n})

where createData has a tabid value greater than any current tab (and you can find the greatest current tabid using chrome.windows.getAll()).

However, in terms of doing it on your page (or anywhere that's not a Chrome extension), that's not possible, since whether or not a new window opens in a new tab is determined entirely by the user's settings.

Solution 3

User will have to allow popups but I ended up doing this:

function openMultipleTabs(urlsArray){

    urlsArray.forEach(function(url){
        let link     = document.createElement('a');
        link.href    = url;
        link.target  = '_blank';
        link.click();
    });

}

Solution 4

The best way to open multiple tabs or windows is by using setTimeout() of 500ms.

window.open("https://facebook.com", "one", windowFeatures);
setTimeout(function(){
  window.open("https://facebook.com", "two", windowFeatures);
}, 500);
Share:
70,760

Related videos on Youtube

user3522725
Author by

user3522725

Updated on August 19, 2022

Comments

  • user3522725
    user3522725 over 1 year

    I'm trying to open multiple links at once in Google Chrome in new tabs but it fails.

    Problems:

    1. Blocked by popup
    2. Open in new windows instead of tab after the user allowed the popup

    With this, I can open multiple links at once in Firefox:

    <!DOCTYPE html>
    <html ng-app="plunker">
    
    <head>
        <meta charset="utf-8">
        <title>AngularJS Plunker</title>
        <script>document.write('<base href="' + document.location + '" >');</script>
        <link rel="stylesheet" href="style.css">
        <script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
        <script src="app.js"></script>
    </head>
    
    <body ng-controller="MainCtrl">
        <button ng-click="openLinks()">Open</button>
    </body>
    
    </html>
    

    Also, I came across someone who found a workaround.

    I tried using setInterval to try to open the links individually but it didn't work.

    • Admin
      Admin almost 10 years
      This seems a little iffy, you're trying to bypass popup blocks even though they were made for this specific reason, to block popups... :L
    • user3522457
      user3522457 almost 10 years
      I'm not doing some fishy stuff.. look at the extension, how the developer bypassed it? hmm
    • Patrick Collins
      Patrick Collins almost 10 years
      Do you want to do that independently of browser settings? I don't think that that's possible.
  • Maverick
    Maverick almost 10 years
    While it may be interesting to look at the code of a browser extension, this code WILL NOT WORK on a website.
  • Maverick
    Maverick almost 10 years
    This is correct. Browser extensions can do a lot more than a webpage can. Despite both using javascript, they are very, very different things.
  • Patrick Collins
    Patrick Collins almost 10 years
    chrome.tabs.create is part of the Chrome API that I discussed in my answer -- it's not going to work on a webpage.
  • nyzm
    nyzm almost 10 years
    I know it won't work in a webpage since it uses chrome api. That was for answering his question about how extension developer achieved it.
  • Arthur
    Arthur over 8 years
    "vanilla JavaScript" works nicely in your example. but a page with multiple open_win functions (and corresponding buttons) does not open multiple tabs for each button:
  • Kaddath
    Kaddath over 6 years
    the "vanilla JavaScript" is no longer working in chrome (January 2018), it's opening a single tab, not two..
  • tfantina
    tfantina almost 6 years
    As of July 2018 I believe the best solution is something like this: window.open("https://www.ecosia.org", "_new"); window.open("https://www.duckduckgo.com", "secondWindow"); The trick is to make sure that the second window has a name other than _new otherwise the browser will try to stick the link into the same window overriding the first window.open. I have tried this in Chrome and IE and it works for me.
  • Ethan Allen
    Ethan Allen over 4 years
    As of Nov 2019, tfantina's code only opens one tab on mobile Safari (iOS 13.2). Works fine on Mac Safari (13.0.3) though.
  • Flimm
    Flimm over 4 years
    This only opens it in one tab for me.
  • Serge
    Serge about 3 years
    Could you give a test reference where such a solution works ? Tnx in advance
  • Don Kartacs
    Don Kartacs about 3 years
  • Revisto
    Revisto almost 3 years
    dude, your second solution was awesome.