HTML 按钮关闭窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8057802/
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-29 11:26:00  来源:igfitidea点击:

HTML Button Close Window

html

提问by jcmitch

I have an html button that I want to close the current window when clicked. I thought I could just set the onclick feature like I did below. Am I missing something?

我有一个 html 按钮,我想在单击时关闭当前窗口。我以为我可以像下面那样设置 onclick 功能。我错过了什么吗?

<button type="button" onclick="javascript:window.close()">Discard</button>

回答by amosrivera

When in the onclickattribute you do not need to specify that it is Javascript.

当在onclick属性中时,您不需要指定它是 Javascript。

<button type="button" 
        onclick="window.open('', '_self', ''); window.close();">Discard</button>

This should do it. In order to close it your page needs to be opened by the script, hence the window.open. Here is an article explaining this in detail:

这应该做。为了关闭它,您的页面需要由脚本打开,因此 window.open。这里有一篇文章详细解释了这一点:

Click Here

点击这里

If all else fails, you should also add a message asking the user to manually close the window, as there is no cross-browser solution for this, especially with older browsers such as IE 8.

如果一切都失败了,您还应该添加一条消息,要求用户手动关闭窗口,因为没有跨浏览器的解决方案,尤其是对于 IE 8 等较旧的浏览器。

回答by rodrigo-silveira

JavaScript can only close a window that was opened using JavaScript. Example below:

JavaScript 只能关闭使用 JavaScript 打开的窗口。下面的例子:

<script>
function myFunction() {
  var str = "Sample";
  var result = str.link("https://sample.com");
  document.getElementById("demo").innerHTML = result;
}
</script>

回答by Anonymous

Use the code below. It works every time.

使用下面的代码。它每次都有效。

<button onclick="self.close()">Close</button>

It works every time in Chrome and also works on Firefox.

它每次都适用于 Chrome,也适用于 Firefox。

回答by KenP

This site: http://www.computerhope.com/issues/ch000178.htmanswers it with the script below

这个站点:http: //www.computerhope.com/issues/ch000178.htm用下面的脚本回答它

< input type="button" value="Close this window" onclick="self.close()">

< input type="button" value="关闭这个窗口" onclick="self.close()">

回答by Brian T

I know this thread has been answered, but another solution that may be useful for some, particularly to those with multiple pages where they want to have this button, is to give the input an id and place the code in a JavaScript file. You can then place the code for the button on multiple pages, taking up less space in your code.

我知道该线程已得到解答,但另一种可能对某些人有用的解决方案,特别是对于那些想要拥有此按钮的多个页面的人,是为输入提供一个 id 并将代码放在 JavaScript 文件中。然后,您可以将按钮的代码放在多个页面上,从而在代码中占用更少的空间。

For the button:

对于按钮:

    <input type="button" id="cancel_edit" value="Cancel"></input>

in the JavaScript file:

在 JavaScript 文件中:

    $("#cancel_edit").click(function(){
        window.open('','_parent',''); 
        window.close(); 
    });

回答by Ozgar

Closing a window that was opened by the user through JavaScript is considered to be a security risk, thus not all browsers will allow this (which is why all solutions are hacks/workarounds). Browsers that are steadily maintained remove these types of hacks, and solutions that work one day may be broken the next.

关闭用户通过 JavaScript 打开的窗口被认为存在安全风险,因此并非所有浏览器都允许这样做(这就是为什么所有解决方案都是 hacks/workarounds)。稳定维护的浏览器会删除这些类型的黑客行为,一天有效的解决方案可能会在下一天被破坏。

This was addressed hereby rvighnein a similar question on the subject.

这是写给这里通过rvighne关于这个问题类似的问题。

回答by LesD

November 2019: onclick="self.close()"still works in Chrome while Edge gives a warning that must be confirmed before it will close.

2019 年 11 月: onclick="self.close()"仍可在 Chrome 中使用,而 Edge 会发出警告,必须在关闭之前确认。

On the other hand the solution onclick="window.open('', '_self', ''); window.close();"works in both.

另一方面,该解决方案onclick="window.open('', '_self', ''); window.close();"适用于两者。