[英]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.