关闭浏览器窗口深入研究

一,研究的起因

 

今天中午左右,Software@China群内朋友问:如何关闭firefox窗口,笔者尝试几次,均未能成功关闭firefox窗口,

 

二,测试浏览器

 

1,chrome5.0

2,ie7

3,opera10.5

4,safari5.0

 

三,测试

 

序号 关闭代码 需要确认 无任何作用 无需确认 测试
1window.close() IE7 firefox,chrome,
safari
Opera Close
2window.opener=null;
window.open('','_self');
window.close();
firefox IE7,Opera,
chrome,safari
Close
3window.open('','_self');
window.close();
firefox IE7,Opera,
chrome,safari
Close
4window.opener=null;
window.close();
IE7 firefox,safari chrome,Opera Close
5var opened=window.open('about:blank','_self');
opened.opener=null;
opened.close();
firefox safari,IE7,
chrome,Opera
Close
6var opened=window.open('about:blank','_self');
opened.close();
safari,firefox IE7,
chrome,Opera
Close

 

 

 

 

 

测试源码:

 

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>关闭浏览器窗口深入研究 by sunflower</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
	<script>
	</script>
 </HEAD>
 <BODY>
	<table border="1" CELLPADDING="0" CELLSPACING="0">
		<tr height="15px">
			<th >
				序号
			</th>
			<th >
				关闭代码
			</th>
			<th width="70px">
				需要确认
			</th>
			<th>
				无任何作用
			</th>
			<th>
				无需确认
			</th>
			<th>
				测试
			</th>
		</tr>
		<tr height="25px">
			<td>1</td>
			<td>
				window.close()
			</td>
			<td width="20px"  align="center">
				IE7
			</td>
			<td align="center" >
				firefox,chrome,<br />safari
			</td>
			<td align="center" >
				Opera
			</td>
			<td width="80px" align="center">
				<a href="javascript:window.close()">Close</a>
			</td>
		</tr>
		<tr height="25px">
			<td>2</td>
			<td>
				window.opener=null;<br />window.open('','_self');<br />window.close();
			</td>
			<td width="20px"  align="center">
				&nbsp;
			</td>
			<td align="center" >
				firefox
			</td>
			<td align="center" >
				IE7,Opera,<br />chrome,safari
			</td>
			<td width="80px" align="center">
				<a href="javascript:window.opener=null;window.open('','_self');window.close();">Close</a>
			</td>
		</tr>
		<tr height="25px">
			<td>3</td>
			<td >
				window.open('','_self');<br />window.close();
			</td>
			<td width="20px"  align="center">
				&nbsp;
			</td>
			<td align="center" >
				firefox
			</td>
			<td align="center" >
				IE7,Opera,<br />chrome,safari
			</td>
			<td width="80px" align="center">
				<a href="javascript:window.open('','_self');window.close();">Close</a>
			</td>
		</tr>
		<tr height="25px">
			<td>4</td>
			<td>
				window.opener=null;<br />window.close();
			</td>
			<td width="20px"  align="center">
				IE7
			</td>
			<td align="center" >
				firefox,safari
			</td>
			<td align="center" >
				chrome,Opera
			</td>
			<td width="80px" align="center">
				<a href="javascript:window.opener=null;window.close();">Close</a>
			</td>
		</tr>

		<tr height="25px">
			<td>5</td>
			<td>
				var opened=window.open('about:blank','_self');<br />opened.opener=null;<br />opened.close();
			</td>
			<td width="20px"  align="center">
				
			</td>
			<td align="center" >
				firefox
			</td>
			<td align="center" >
				safari,IE7,<br />chrome,Opera
			</td>
			<td width="80px" align="center">
				<a href="javascript:var opened=window.open('about:blank','_self');<br />opened.opener=null;<br />opened.close();">Close</a>
			</td>
		</tr>

		<tr height="25px">
			<td>6</td>
			<td>
				var opened=window.open('about:blank','_self');
				<br />opened.close();
			</td>
			<td width="20px"  align="center">
				
			</td>
			<td align="center" >
				safari,firefox
			</td>
			<td align="center" >
				firefox,IE7,<br />chrome,Opera
			</td>
			<td width="80px" align="center">
				<a href="javascript:var opened=window.open('about:blank','_self');opened.close();">Close</a>
			</td>
		</tr>
	</table>
	
 </BODY>
</HTML>
 

 

 

 

四,结论

 

1,firefox如果未开启允许window.close关闭窗口,window.close无任何效果,即使使用window.open切换窗口,获得控制权,firefox仍然不允许你关闭窗口

 

2,从(三)中的结论得知,(三)2、(三)3、(三)5 代码兼容性更好一些,除了firefox外,均可无提示关闭

 

3,比较(三)2、(三)3、(三)5 ,其中(三)2、(三)3 对于frefox无任何反应,而(三)5会打开一个about:blank的空白页,给用户的感觉是:用户的操作有了回应,虽然并不完美,但原窗口没有了!

 

 

4,opera对于javascript关闭窗口,基本没有安全限制,在chrome,IE,firefox认为危险的操作,opera不会做任何提示!

 

 

 

-----------------------------------------------------------------------------------------

 

 

 

 

 

如果您发现更好的方法,留言或者联系我:QQ 527067272,或者email: zhangxuehuaemail # gmail dot com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值