[英]close firefox tabs with javascript
I have a javascript code which opens a new tab in browser from a list each 50seconds, but the browser will crash after 50tabs or more. 我有一个javascript代码,每隔50秒就会从列表中打开一个新标签页,但50tabs或更长时间后浏览器将崩溃。 so I want to close new tab and open another one each 50 seconds. 所以我想关闭新标签并每50秒打开另一个标签。
my code is: 我的代码是:
<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
returns a reference to the new window. window.open
返回对新窗口的引用。 Call close
on that handle. 在该句柄上调用close
。
https://developer.mozilla.org/en-US/docs/Web/API/window.close https://developer.mozilla.org/zh-CN/docs/Web/API/window.close
note btw: 注意:
FAQ 常问问题
How can I prevent the confirmation message asking the user whether he wants to close the window? 如何防止确认消息询问用户是否要关闭窗口? You can not. 你不能。 New windows not opened by javascript can not as a rule be closed by JavaScript. 通常,JavaScript无法关闭未由javascript打开的新窗口。 The JavaScript Console in Mozilla-based browsers will report the warning message: "Scripts may not close windows that were not opened by script." 基于Mozilla的浏览器中的JavaScript控制台将报告警告消息:“脚本可能无法关闭脚本未打开的窗口。” Otherwise the history of URLs visited during the browser session would be lost. 否则,将丢失浏览器会话期间访问的URL的历史记录。
window.close() should work to close tabs you opened (but it's not possible to close a tab you didn't open) window.close()应该可以关闭您打开的标签页(但无法关闭未打开的标签页)
in fact it can't be closed unless it was opened by a script but there is a way to fool the browser into thinking that's the case: 实际上,除非通过脚本将其打开,否则无法将其关闭,但是有一种方法可以使浏览器欺骗这种情况:
window.open('','_parent','');
then you can use 那么你可以使用
window.close();
Putting it together: 把它放在一起:
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>
reference: http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html 参考: http : //www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html
要关闭Firefox标签,请使用window.close(),这将关闭当前窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.