2014-02-23 131 views
0

我有一个JavaScript代码,它在每个50秒的列表中打开浏览器中的新选项卡,但浏览器会在50个或更多后崩溃。 所以我想关闭新标签并每50秒打开一个标签。使用javascript关闭Firefox选项卡

我的代码是:

<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> 
+2

不知何故,我看不到你的网站很受欢迎... –

回答

0

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

https://developer.mozilla.org/en-US/docs/Web/API/window.close

注BTW:

FAQ

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

0

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

+0

我应该在自己的代码中做什么? – user3051479

0

关闭一个Firefox标签使用 window.close()的 这将关闭当前窗口。

相关问题