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 badges3 Answers
Reset to default 1When 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:
- This will not bypass pop-up blockers. Users must allow it.
- 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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745640884a4637684.html
评论列表(0条)