最近做项目的时候碰到一个问题:
在js里面使用window.close(),IE下工作正常,但是FireFox下有时可以,有时不行。
搜索发现,可以这样解决:
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>
<a href="javascript:closeWindow();">Close Window</a>
在前面加一句 window.open('','_parent','');
作用为:
fool the browser into thinking that it was opened with a script
This opens a new page, (non-existent), into a target
frame/window, (_parent which of course is the window in
which the script is executed, so replacing itself),
and defines parameters such as window size etc, (in
this case none are defined as none are needed). Now
that the browser thinks a script opened a page we
can quickly close it in the standard way…