最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

google chrome - Open multiple urls in same new tab javascript - Stack Overflow

matteradmin9PV0评论

I am writing a javascript open multiple URLs on the same tab. I know how to open a link in a new tab.

window.open(your_url,"_blank")

However, my client wants me to open just one tab with multiple URLs. Imagine you have a javascript

var urlList=['', 'www.youtube']

I want to open then one by one on the same new tabs with interval 10secs. First, I do

window.open(urlList[0],"_blank")

But then if I am still doing that for the second one, it opens another new tab, not on the old one. Any knows how to specify the opened tab?

I am writing a javascript open multiple URLs on the same tab. I know how to open a link in a new tab.

window.open(your_url,"_blank")

However, my client wants me to open just one tab with multiple URLs. Imagine you have a javascript

var urlList=['https://www.google.', 'www.youtube.']

I want to open then one by one on the same new tabs with interval 10secs. First, I do

window.open(urlList[0],"_blank")

But then if I am still doing that for the second one, it opens another new tab, not on the old one. Any knows how to specify the opened tab?

Share Improve this question asked Apr 1, 2019 at 14:35 Lbj_xLbj_x 4155 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

When you are opening using window.open method it will return the window object of the newly opened tab, use it for updating URL after 10 seconds. For providing delay use setInterval method.

// website lists
const urlList = ['https://www.google.', 'http://www.youtube.']

// open the first url and cache the window object reference
const win = window.open(urlList[0], "_blank")

// variable for keeping track of array position(urls)
let i = 1;

// create interval with 10seconds delay and keep 
// interval reference to clear the event in future
let int = setInterval(() => {
  // update the location with next array value
  win.location = urlList[i];
  // check value of i and increment, if reached the max value then clear the interval
  if (i++ >= urlList.length) clearInterval(int)
}, 10000)

this is a sample code.

    async function navigate() {
 var _window = window.open("","_blank")
 var urlList=['https://www.google.', 'https://www.youtube.'];   
 for (var url of urlList) {     
    _window.location.replace(url);
    await sleep(10000);
 }   
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

I hope this help you.

I don't know why you'd want to do this. Don't. It's super annoying. But if you must, the following works and has been tested in chrome (and only in chrome)

A couple of things to note:

  1. This will not bypass pop-up blockers. Users must allow it.
  2. This does not technically use the "same" tab. Old one is closed and new opened quick enough user won't notice.

var myWindow;

let urls = ["https://stackoverflow.", "https://stackexchange./"];
let counter = 0;
let openWindow;

function openWin(url) {
    openWindow = window.open(url, "_blank");
}

function closeWin(){
    openWindow.close();
}

setInterval(function(){
    if(openWindow) closeWin();
    openWin(urls[counter]);
    counter++;
}, 10000)

Post a comment

comment list (0)

  1. No comments so far