Brian Barry
Brian Barry

Reputation: 559

How to open new window with multiple tabs with javascript?

I would like to open a link in a new window that opens multiple tabs. Currently, I am able to to open multiple tabs in the same window; this looks something like:

$('a.yourlink').click(function(e) {
      e.preventDefault();
      window.open('http://yahoo.com');
      window.open('http://google.com');
  

So clicking a link of class 'yourlink' will open multiple links on the same window. For doing the same thing, but in a new normal window, I have tried using the following jquery arguments window.open('url', 'window name', 'window settings') in the first window.open. However this only makes one link open in a new window, and also in an undesirable format.

Upvotes: 2

Views: 2723

Answers (1)

Pranav Rustagi
Pranav Rustagi

Reputation: 2731

You can achieve that, by doing this :

Let's say that your current file is index.html.

Step 1 : Create a new file, say, in same folder as of index.html. (let's say temp.html).

Step 2 : Now in the JavaScript code index.html, write :

$('a.yourlink').click(function(e) {
    e.preventDefault();
    window.open('./temp.html', '_blank', 'location=yes,scrollbars=yes,status=yes');
});

This will open a new window.

Step 3 : Just add script tag in temp.html, and write following code inside it:

var links = ['http://yahoo.com', 'http://google.com'];
for(let i = 0 ; i < links.length ; i++){
   window.open(links[i]);
}
window.close();

In the array, put all the links you want to get opened.

Upvotes: 1

Related Questions