25

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="angular.js@1.2.x" 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.

undefined
  • 1,019
  • 12
  • 24
user3522725
  • 387
  • 1
  • 4
  • 13
  • 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 –  Jun 23 '14 at 11:00
  • I'm not doing some fishy stuff.. look at the extension, how the developer bypassed it? hmm – user3522457 Jun 23 '14 at 14:13
  • Do you want to do that [independently of browser settings](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript)? I don't think that that's possible. – Patrick Collins Jun 26 '14 at 00:53

9 Answers9

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

Andrew
  • 995
  • 6
  • 10
  • "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: – Arthur Nov 17 '15 at 19:05
  • 18
    the "vanilla JavaScript" is no longer working in chrome (January 2018), it's opening a single tab, not two.. – Kaddath Jan 08 '18 at 13:00
  • 5
    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. – tfantina Jul 13 '18 at 14:39
  • 2
    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. – Ethan Allen Nov 17 '19 at 02:53
  • dude, your second solution was awesome. – Revisto May 20 '21 at 15:59
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.

Community
  • 1
  • 1
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
  • 2
    This is correct. Browser extensions can do a lot more than a webpage can. Despite both using javascript, they are very, very different things. – Maverick Jun 26 '14 at 01:10
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);
Flimm
  • 136,138
  • 45
  • 251
  • 267
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

Juraj Bublinec
  • 410
  • 4
  • 11
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.

Raihan Ridoy
  • 678
  • 8
  • 18
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();
    });

}
Ale W
  • 29
  • 6
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);
    }
  });
}
-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

nyzm
  • 2,787
  • 3
  • 24
  • 30
  • While it may be interesting to look at the code of a browser extension, this code WILL NOT WORK on a website. – Maverick Jun 26 '14 at 01:08
  • `chrome.tabs.create` is part of the Chrome API that I discussed in my answer -- it's not going to work on a webpage. – Patrick Collins Jun 26 '14 at 02:20
  • 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. – nyzm Jun 26 '14 at 02:28
-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.

Don Kartacs
  • 396
  • 3
  • 8