2014-02-23 39 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()的 這將關閉當前窗口。

相關問題