繁体   English   中英

使用javascript关闭Firefox标签

[英]close firefox tabs with javascript

我有一个javascript代码,每隔50秒就会从列表中打开一个新标签页,但50tabs或更长时间后浏览器将崩溃。 所以我想关闭新标签并每50秒打开另一个标签。

我的代码是:

<html>
<head>
<script>
function openWindow(){
    window.open('"about:blank"');
    var x = document.getElementById('a').value.split('\n');
    atTime = 0;
    for (var i = 0; i < x.length; i++) {
      if (x[i].indexOf('.') > 0) {
        site = x[i];
        if (x[i].indexOf('://') < 0) { site = 'http://' + x[i]; }
        setTimeout("window.open('" + site + "')", atTime);
        atTime += 50000;
      }
    }
}
</script>
<style>
html, body
{
    height : 99%;
    width  : 99%;
}

textarea
{
    height : 80%;
    width  : 90%;
}
</style>
</head>
<body>
<textarea id="a"></textarea>
<br>
<input type="button" value="Open Windows" onClick="openWindow()">
<input type="button" value="Clear" onClick="document.getElementById('a').value=''">
</body>
</html>

window.open返回对新窗口的引用。 在该句柄上调用close

https://developer.mozilla.org/zh-CN/docs/Web/API/window.close

注意:

常问问题

如何防止确认消息询问用户是否要关闭窗口? 你不能。 通常,JavaScript无法关闭未由javascript打开的新窗口。 基于Mozilla的浏览器中的JavaScript控制台将报告警告消息:“脚本可能无法关闭脚本未打开的窗口。” 否则,将丢失浏览器会话期间访问的URL的历史记录。

window.close()应该可以关闭您打开的标签页(但无法关闭未打开的标签页)

实际上,除非通过脚本将其打开,否则无法将其关闭,但是有一种方法可以使浏览器欺骗这种情况:

window.open('','_parent','');

那么你可以使用

window.close();

把它放在一起:

<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script> 

参考: http : //www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html

要关闭Firefox标签,请使用window.close(),这将关闭当前窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM