Open multiple links in Chrome at once as new tabs
Asked Answered
A

10

26

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.

Aflutter answered 23/6, 2014 at 10:54 Comment(3)
This seems a little iffy, you're trying to bypass popup blocks even though they were made for this specific reason, to block popups... :LBelieve
I'm not doing some fishy stuff.. look at the extension, how the developer bypassed it? hmmWinze
Do you want to do that independently of browser settings? I don't think that that's possible.Denote
T
44

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

Trysail answered 26/6, 2014 at 16:37 Comment(5)
"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:Corneous
the "vanilla JavaScript" is no longer working in chrome (January 2018), it's opening a single tab, not two..Luxury
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.Lipski
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.Cerallua
dude, your second solution was awesome.Aalii
D
7

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.

Denote answered 26/6, 2014 at 0:58 Comment(1)
This is correct. Browser extensions can do a lot more than a webpage can. Despite both using javascript, they are very, very different things.Project
R
3

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);
Republic answered 30/9, 2019 at 8:36 Comment(0)
O
3

Worth mentioning that you need to actually have popups allowed in your browser settings. Don't rely on browser alert asking you if you want to allow the popup to open.

enter image description here

Oyster answered 23/2, 2022 at 6:2 Comment(1)
Save the day, thank you !Kaminsky
E
1

The following code will open multiple popUp on the button click.

<html>
<head>
<title></title>
<script type="text/javascript">
  function open_win() {
 window.open("url","windowName","windowFeatures")
 window.open("url","DifferentWindowName","windowFeatures")// different name for each popup
}
</script>
</head>

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

you need to make sure that each window name is different, otherwise the last popup will overwrite it's previous popup. As a result you will end up with a single popup.

Esmond answered 10/7, 2018 at 8:40 Comment(0)
L
1

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();
    });

}
Lightproof answered 16/9, 2019 at 14:44 Comment(1)
This only opens it in one tab for me.Selfopinionated
F
0

I have a simple solution playing with setTimeout, check below

function openMultipleTabs(urlsArray: string[]) {
  urlsArray.forEach((url: string, key: number) => {
    if (key === 0) {
      window.open(url, `_blank_first_${key.toString()}`);
    } else {
      setTimeout(function () {
        console.log("resolved", key);
        window.open(url, `_blank_${key.toString()}`);
      }, 1500 * key);
    }
  });
}
Frisian answered 5/8, 2021 at 9:59 Comment(0)
B
-1

Looks like extension uses below code to open those tabs.

function openTab(urls, delay, window_id, tab_position, close_time) {
    var obj = {
            windowId: window_id,
            url: urls.shift().url,
            selected: false
    }

    if(tab_position != null) {
        obj.index = tab_position
        tab_position++;
    }

    chrome.tabs.create(obj, function(tab) {
        if(close_time > 0) {
            window.setTimeout(function() {
                chrome.tabs.remove(tab.id);
            }, close_time*1000);
        }
    });

    if(urls.length > 0) {
        window.setTimeout(function() {openTab(urls, delay, window_id, tab_position, close_time)}, delay*1000);
    }

}

If you want to take a look at the code of the extension for reference you will find the extensions in (for Windows) C:\Documents and Settings\*UserName*\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions

Brachycephalic answered 26/6, 2014 at 1:2 Comment(3)
While it may be interesting to look at the code of a browser extension, this code WILL NOT WORK on a website.Project
chrome.tabs.create is part of the Chrome API that I discussed in my answer -- it's not going to work on a webpage.Denote
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.Brachycephalic
D
-1

Since modern browsers (and even old ones with blockers), will absolutely not allow this (one user action, one new tab). My solution was:

    openInfoLinks = () => {
        const urlsArray = [
            `https://...`,
            `https://...`,
            `https://...`,
        ]
        window.open(
            urlsArray[this.linkCounter],
            `_blank_${someIdentifier}_${this.linkCounter}`
        );
        this.linkCounter++;
        setTimeout(() => {
            this.linkCounter = 0;
        }, 500);
    }

The user can open the links in quick succession with ctrl+click-ing the button N times.

Doubletime answered 29/9, 2020 at 8:49 Comment(2)
Could you give a test reference where such a solution works ? Tnx in advanceHamsun
sure, codesandbox.io/s/young-architecture-7nzot?file=/src/App.jsDoubletime
C
-1

Open Developer console by hitting F12 on your keyboard, and then enter the following code:

var str = `
https://google.com
https://google.com/mail
https://google.com/images
`;

var linkArray = str.split(/\r?\n/);
for (var i = 0; i < linkArray.length; i++) {
    if (linkArray[i].length > 1) {
        // will open each link in the current window
        window.open(linkArray[i], '_blank')
    }
}
Collegiate answered 5/5 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.