javascript關閉窗口(兼容firefox,IE)
方法一:
- function CloseWin() //這個不會提示是否關閉瀏覽器
- {
- window.opener=null;
- //window.opener=top;
- window.open("","_self");
- window.close();
- }
open.htmljs 代碼
- function open_complex_self() {
- var obj_window = window.open('close.html', '_self');
- obj_window.opener = window;
- obj_window.focus();
- }
close.htmljs 代碼
- window.close();
另附:
//普通帶提示關閉function closeie(){ window.close(); }//關閉IE6不提示 function closeie6(){ window.opener=null; window.close(); }//關閉IE7不提示 function closeie7(){ window.open('','_top'); window.top.close(); }
javascript關閉窗口,可以用下面簡單的代碼:
<a href="javascript:self.close()">關閉窗口</a>
我在IE7下測試通過,但是firefox3.0卻不行。
難道firefox不支持在href中直接寫JavaScript?于是改成下面的樣子:
<a href="javascript:alert('Hello World')">彈出窗口</a>
這次IE7和firefox下測試都通過。那就不是href中直接寫JavaScript的原因了。
繼續(xù)測試firefox怎么關閉自身窗口
改成了如下代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--
function windowClose(){
//self.close();
window.close();
}
//-->
</script>
<title>js測試</title>
</head>
<a href="javascript:self.close()">關閉窗口</a><br />
<a href="javascript:alert('Hello World')">彈出窗口</a><br />
<a href="#" onclick="windowClose()">js函數(shù)關閉窗口</a>
<body>
</body>
</html>
還是不能關閉窗口。難道firefox不支持window的close屬性?
那window對象的close方法能不能關閉open方法打開的窗口呢?
寫下面兩個html文件放在同一個文件夾下
1.open.html
<script type="text/javascript">
<!--
function openWindow(){
window.open("new.html","newWindow","width=200,height=100,toolbar=no");
}
//-->
</script>
<a href="#" onclick="openWindow()">open函數(shù)打開新窗口</a><br />
<a href="new.html" target="_blank">超級鏈接在新窗口中打開新頁面</a><br />
<a href="new.html" target="_parent">超級鏈接在父窗口中打開新頁面</a>
2.new.html
<a href="javascript:window.close()">關閉窗口</a>
<a href="javascript:self.close()">關閉窗口</a>
用open方法和在"_blank"打開的可以在新窗口中關閉,而在"_parent"中打開的在firefox中還是關閉不
了
因此在firefox里用window的close方法時要注意他和IE不同的地方:在父窗口打開的頁面是不能用close
方法關閉的。
然后去google搜了一下:之所以window.close在firefox不能使用,是因為firefox默認不能關閉用戶打
開的網(wǎng)頁,我們可以這樣設置firefox:
打開firefox,在地址欄輸入about:config
找到dom.allow_scripts_to_close_windows這項并改為true。
現(xiàn)在知道為什么了吧。那篇文章還有一段不錯的內(nèi)容,摘錄如下:
眾所周知,在javascript中window.close()是用來關閉窗口的,而且ie和firefox都是支持的。為了實現(xiàn)
用戶對瀏覽器的絕對控制,ie中用close關閉非open打開的窗口時會彈出一個對話框詢問用戶。有時候我
們不希望再這樣哆嗦,但是怎么去掉這個框呢,用下面的代碼就可以了
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>
<a href="javascript:closeWindow();">Close Window</a>
參考文章:1.http://hi.baidu.com/suen_%CB%EF/blog/item/bedca57f8932480d28388a49.html
2.http://blog.csdn.net/a9529lty/archive/2008/11/22/3351539.aspx
文章出處:DIY部落(http://www.diybl.com/course/1_web/javascript/jsjs/20090318/162531.html)
posted on 2009-04-24 13:35 小卓 閱讀(2299) 評論(0) 編輯 收藏 所屬分類: html and js