Javascript 如何使用 jQuery 关闭窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8890394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:56:48  来源:igfitidea点击:

How to close a window using jQuery

javascriptjquerycross-browser

提问by surya

How to close a window using jQuery for all browser compatibility?

如何使用 jQuery 关闭窗口以实现所有浏览器兼容性?

<input type="button" 
       name="backButton" 
       value="Close" 
       style="background-color:#245f91; color:#fff;font-weight:bold;" 
       onclick="window.close();">

This onclickevent is working in IE. Could you help me to write code in jQuery?

onclick事件在 IE 中有效。你能帮我用jQuery写代码吗?

回答by Shiplu Mokaddim

$(element).click(function(){
    window.close();
});

Note: you can notclose any window that you didn't opened with window.open. Directly invoking window.close()will ask user with a dialogue box.

注意:您不能关闭任何不是用window.open. 直接调用window.close()会用对话框询问用户。

回答by Sboniso Marcus Nzimande

You can only use the window.closefunction when you have opened the window using window.open(), so I use the following function:

您只能在使用window.close打开窗口时使用该功能window.open(),因此我使用以下功能:

function close_window(url){
    var newWindow = window.open('', '_self', ''); //open the current window
    window.close(url);
 }

回答by liunian

just window.close()is OK, why should write in jQuery?

刚刚window.close()好,为什么要用jQuery来写?

回答by Bajju

For IE: window.close();and self.close();should work fine.

对于 IE:window.close();并且self.close();应该可以正常工作。

If you want just open the IE browser and type

如果你只想打开IE浏览器并输入

javascript:self.close()and hit enter, it should ask you for a prompt.

javascript:self.close()然后按回车键,它应该会要求你提示。

Note: this method doesn't work for Chrome or Firefox.

注意:此方法不适用于 Chrome 或 Firefox。

回答by Zo Has

This will only work for windows which are opened by using window.open(); method. Try this

这仅适用于使用 window.open() 打开的窗口;方法。尝试这个

var tmp=window.open(params);
tmp.close();