2

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.

Brian Barry
  • 439
  • 2
  • 17
  • 1
    The `window` object in your code represents the current window. So whenever you use `window.open` it can only create a new tab in the current window or a fresh new window. But you cannot create a tab in the newly created window – Beingnin Sep 28 '20 at 05:00
  • As far as I know, this is not possible in javascript. [Check this answer](https://stackoverflow.com/a/17887468) – Preeti Rawat Sep 28 '20 at 05:40

1 Answers1

1

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.

Pranav Rustagi
  • 2,604
  • 1
  • 5
  • 18