Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Opening and closing multiple tabs using Javascript on Chrome

I have an anchor tag on my site which on click, should open 4 new tabs using javascript

Anchor Tag: <a onclick="openTabs()"> Open 4 tabsLink </a>

JS Function:

let windows = [];
function openTabs(){
  var locs = [
    'https://www.google.com',
    'https://www.google.com',
    'https://www.google.com',
    'https://www.google.com',
  ]
  for (let i = 0; i < locs.length; i++) {
    windows.push(window.open(locs[i], '_blank'))
  };
}

The user should be able to close all of the 4 tabs by clicking on a single button. The function to close all opened tabs:

function closeTabs(){
      if (windows.length > 0){
        for(var i = 0; i < windows.length; i++){
          windows[i].close();
        }
      }
}

The above code works just fine on Firefox (after I allow pop ups) but on chrome, even the 'openTabs' function doesn't work. It opens only the first link. The closeTabs function throws the below error:

(index):1815 Uncaught TypeError: Cannot read properties of null (reading 'close')

Is there a separate method/API that I should be leveraging to enable this option on chrome? Or is there a more efficient way to do this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!