Using many window.open

Issue

I have a loop and need to open new window in a new tab many times:

items.forEach((item) => {
   window.open(item.url)
})
console.log("here") // Doesn't work

But window.open executes only 1 time.

Also other functions not executing.
What I supposed to do?

Solution

Use '_blank' as second param of window.open("url",'_blank');

or set a specific window title for each item (prevent same because it’ll open in same ):

items.forEach((item) => {
   // below I used url as title , if you have title attribute 
   // you can set item.title instead 
   window.open(item.url,item.url) 
})

For example try pasting below code in console , it should open 2 tabs :
check this in console :

[{"url":"http://www.google.com","title":"google"},{"url":"http://www.stackoverflow.com","title":"stackoverflow"}].forEach((item) => {
    window.open(item.url,'_blank')
    // or use 
    // window.open(item.url,item.title)
});

Answered By – Spring

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published