2014-01-10 72 views
1

我在页面中有一个“关闭”按钮。关闭按钮在Chrome中不工作

单击按钮时,不执行任何操作。

OnClick操作是以window.top.close()的形式给出的。

<input class="CloseButton" type="button" id="CloseButton" value="Close" onclick="window.top.close();"> 

理想情况下,单击按钮时窗口应该关闭或弹出确认框。

我在Internet Explorer 11,ChromeMozilla browsers看到此问题。

关闭功能在Internet Explorer 10和其他较低版本的IE中正常工作。

添加其他详细信息:此窗口是一个从javascript创建的弹出窗口。

+0

,你已经把该关闭按钮' 'page''或'' 一个js popup''? – Jai

+0

如果窗口是用JavaScript打开的,你只能关闭'window'。 –

回答

0

更换window.top.close();你可以尝试

<input class="CloseButton" type="button" id="CloseButton" value="Close" onclick="window.close();"> 
0

它不会由于安全限制的工作。

你可以试试这个脚本:

function closeWindow() { 
    // normal close for ie <= 9 
    self.close(); 

    // hack for chrome and safari 
    window.open('', '_self', ''); 
    window.close(); 
} 

Chrome和IE新版本允许关闭,只有当它已经被打开的Javascript。因此,上面的hack打开一个虚拟窗口,然后尝试关闭它。

注意:上面的黑客将不适用于Firefox!而且也不是面向未来的。

0

试试这个,

<button onclick="openWin()">Open "myWindow"</button> 
<button onclick="closeWin()">Close "myWindow"</button> 

<script> 
var myWindow; 

function openWin() 
{ 
myWindow = window.open("","myWindow","width=200,height=100"); 
myWindow.document.write("<p>This is 'myWindow'</p>"); 
} 

function closeWin() 
{ 
myWindow.close(); 
} 
</script>