最近做項目的時候碰到一個問題:
在js里面使用window.close(),IE下工作正常,但是FireFox下有時可以,有時不行。
搜索發(fā)現(xiàn),可以這樣解決:
<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…