admin管理员组文章数量:1026989
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)
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)
本文标签: google chromeOpen multiple urls in same new tab javascriptStack Overflow
版权声明:本文标题:google chrome - Open multiple urls in same new tab javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745640883a2160739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论