在 Javascript 中关闭窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3670475/
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
Close Window in Javascript
提问by oscarm
I need a way to close the browser (not a pop up) using Javascript. Is it possible?
我需要一种使用 Javascript 关闭浏览器(不是弹出窗口)的方法。是否可以?
回答by BalusC
If the window isn't opened by some JavaScript code from your own domain, then you can't close it in a crossbrowsercompatible way using JavaScript. Only in MSIE you can use window.close(), but this will generate a warning which is however workaroundable by calling window.open('', '_self', '');beforehand. Still, this won't work in Firefox and other decent browsers. And with a good reason. You shouldn't forcibly close windows which the enduser opened itself. This is Bad User Experience.
如果窗口不是由您自己域中的某些 JavaScript 代码打开的,则您无法使用 JavaScript 以跨浏览器兼容的方式关闭它。仅在 MSIE 中您可以使用window.close(),但这会生成警告,但可以通过window.open('', '_self', '');预先调用来解决。尽管如此,这在 Firefox 和其他不错的浏览器中不起作用。并且有充分的理由。您不应该强行关闭最终用户自己打开的窗口。这是糟糕的用户体验。
回答by Sachin Shanbhag
you can use this -
你可以用这个 -
< a href="javascript:window.opener='x';window.close();">Close< /a>
usually when you use window.close() on main browser window, it shows a message saying "Page is trying to close window". To avoid this, set the window.opener to any value. Idea is window.opener should not be null or empty.
通常当您在主浏览器窗口上使用 window.close() 时,它会显示一条消息“页面正在尝试关闭窗口”。为避免这种情况,请将 window.opener 设置为任何值。想法是 window.opener 不应为 null 或为空。
回答by ngroot
The whole browser? Generally, no. You can close the current tab with window.close(), but it will prompt the user before closing the browser (unless it was created with window.open()). This is a security feature; it would be unpleasant if an arbitrary webpage could close your browser unbidden.
整个浏览器?一般来说,没有。您可以使用 关闭当前选项卡window.close(),但它会在关闭浏览器之前提示用户(除非它是使用 来创建的window.open())。这是一项安全功能;如果任意网页可以不经意地关闭您的浏览器,那将是令人不快的。
回答by jrharshath
In general, you can close a window that you yourself created using javascript.
通常,您可以关闭自己使用 javascript 创建的窗口。
You can't close any other window, not even the window in which your page is loaded. I remember that some browser actually ask "This web page is try to close your window. Allow?" or something of this sort. This is not something that you want your end user to see.
您不能关闭任何其他窗口,甚至不能关闭加载页面的窗口。我记得有些浏览器实际上会问“此网页正在尝试关闭您的窗口。允许吗?” 或类似的东西。这不是您希望最终用户看到的。
BTW, if you specify your particular use case, I could suggest alternate solutions.
顺便说一句,如果您指定您的特定用例,我可以建议替代解决方案。
回答by Kedar1442
try this,
试试这个,
<a href="" OnClick="window.close()">[CLOSE]</a>
It'll close window for sure
它肯定会关闭窗口

