1、 不带任何提示关闭窗口的js代码
<a href="javascript:window.opener=nullwindow.open('','_self')window.close()">关闭</a>
2、自定义提示关闭
<script language="javascript">
// 这个脚本是 ie6和ie7 通用的脚本
function custom_close(){
if
(confirm("您确定要关闭本页吗?")){
window.opener=null
window.open('','_self')
window.close()
}
else{}
}
</script>
<input id="btnClose" type="button" value="关闭本页" onClick="custom_close()" />
我们在js中判断能力窗口或页面都离不开window.close()函数了,但是如果要做到兼容所有浏览器实现关闭当前窗口话并不是直接使用window.close()即可解决了。
可兼容所有浏览器关闭当前页面函数:
JS 关闭网页,之前通过以上方式,可以解决Firefox chrome等浏览器不兼容的问题,但是随着浏览器版本升级,现在无效了。会提示:Scripts may close only the windows that were opened by it.
2
那么如下写法可以解决该问题: 即,如果是以下浏览器,则设置当前页为空白页。
$("#closeBtn").click(function(){
var userAgent = navigator.userAgent
if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {
window.location.href="about:blank"
} else {
window.opener = null
window.open("", "_self")
window.close()
}})
})