Firefox火狐浏览器默认情况下,windows.close()对于非window.open的页面无效,需要windows.close()生效需要修改浏览器的配置,即在firefox浏览器的地址栏输入:about:config然后找到dom.allow_scripts_to_close_windows;把false改为true,但是这对于网站来说,不可能去修改用户的浏览器设置的,因此还是要通过前端代码解决。
既然Firefox浏览器不支持window.close(),但是可以通过打开新空白选项卡about:blank的方式实现关闭当前选项卡,虽然不是真正意义上的关闭当前窗口,但也算是伪关闭吧。
javascript代码:
function pageClose(){
if (navigator.userAgent.indexOf("MSIE") > 0) {
if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
window.opener = null;
window.close();
} else {
window.open('', '_top');
window.top.close();
}
}
else if (navigator.userAgent.indexOf("Firefox") > 0) {
window.location.href = 'about:blank ';
} else {
window.opener = null;
window.open('', '_self', '');
window.close();
}
}
关闭按钮html代码:
附:window.close();对于各浏览器窗口关闭的支持情况
序号
关闭代码
需要确认
无任何作用
无需确认
1
window.close()
IE7
firefox,chrome,
safari
Opera
2
window.opener=null;
window.open('','_self');
window.close();
firefox
IE7,Opera,
chrome,safari
3
window.open('','_self');
window.close();
firefox
IE7,Opera,
chrome,safari
4
window.opener=null;
window.close();
IE7
firefox,safari
chrome,Opera
5
var opened=window.open('about:blank','_self');
opened.opener=null;
opened.close();
firefox
safari,IE7,
chrome,Opera
6
var opened=window.open('about:blank','_self');
opened.close();
safari,firefox
firefox,IE7,
chrome,Opera