最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)

发布评论

评论列表(0)

  1. 暂无评论