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?