![](/img/trans.png)
[英]Opening a new tab from current tab context (when to close the popup)?
[英]Calling function from current tab in a new tab
我写了以下代码:
function challenge() {
var body = $("body").html();
if(body.match(/td\>([0-9]+)\<\/td\>/)[1] > 0){
// find challengable player
var url = body.match(/href="(\/game.php?.*challenge.*[0-9a-f]+)"/)[1].replace(/&/g, '&');
// open new tab with the link
var winEvent = window.open(url, '_blank');
// get random number between 7 and 10
var rand = Math.random() * (10 - 7) + 7;
// set interval for new tab to random minutes
winEvent.setInterval(challenge, rand*1000);
// close current tab
window.close();
}
}
window.setInterval(challenge, 5*1000);
在Challenge()中,我寻找一个链接并在新选项卡中将其打开。 但是,以下行无法正常工作:
winEvent.setInterval(challenge, rand*1000);
新选项卡应每rand秒调用一次Challenge()函数
提前致谢!
好的,根据您的注释和给定的代码。 我仍然是一个小困惑。 但是据我在这里的了解,您想打开一个新选项卡,而在该新选项卡中您想打开另一个新选项卡。
这是一个通用的解决方案。 您可以根据需要进行更改。
要在小提琴中进行测试,您需要允许弹出窗口阻止程序和广告阻止程序。
<html>
<body>
<div>
hello
</div>
<script>
function openTab() {
var newWindow = window.open(); //CREATE A NEW TAB
//WRITE CURRENT OUTER HTML TO THE NEW TAB (BASICALLY CLONING THE CURRENT TAB)
newWindow.document
.write(document.getElementsByTagName("html")[0].outerHTML)
window.close();
}
setTimeout(() => {
openTab();
}, 1000); //OPEN TAB AFTER 1 SECOND
</script>
</body>
</html>
注意:这不是一个好的设计或结构,因为弹出窗口阻止程序或广告阻止程序会阻止您打开新标签页。 据我说,我不建议这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.