gpt4 book ai didi

HTML Button Close Window(超文本标记语言按钮关闭窗口)

转载 作者:bug小助手 更新时间:2023-10-28 09:23:33 30 4
gpt4 key购买 nike



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>

更多回答
优秀答案推荐

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

当在onClick属性中时,您不需要指定它是Java脚本。



<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等较旧的浏览器中。



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>


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上运行。



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打开的窗口被认为是一种安全风险,因此并不是所有的浏览器都允许这样做(这就是为什么所有的解决方案都是黑客/解决办法)。稳定维护的浏览器会清除这些类型的黑客攻击,而一天起作用的解决方案可能会在第二天就被破坏。



This was addressed here by rvighne in a similar question on the subject.

在这里,rvilhne在关于这个主题的类似问题中谈到了这一点。



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

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



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



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();
});


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.lose();”在两者中都适用。


更多回答

Thanks that is good advice but the actual problem is that I'm a moron. Wasted tons of time because I was testing a different file from the one I was editing. Wow... Thanks for the good advice though!

谢谢,这是个好建议,但实际问题是我是个笨蛋。我浪费了大量的时间,因为我测试的文件与我正在编辑的文件不同。哇哦..。不过,还是要谢谢你的好建议!

nice... I'll have to use this next time I have to write a script that creates 130 new windows

不错...下次我写脚本创建130个新窗口的时候,

Well that sure seems to circumvent the entire point of not allowing a raw window.close() call...

这似乎完全绕过了不允许原始window.lose()调用的全部要点...

Doesn't work for me in Chrome 72.0.3626.121, 64-bit. Also, to note, the linked article uses _parent instead of _self. But neither worked per my testing on a local development page -- when the page was opened directly in Chrome, not via script or link from another page. However, works in Edge! :) But window.close() alone also works -- with a confirmation.

在Chrome 72.0.3626.121中不适用,64位。另外,要注意的是,链接的文章使用_parent而不是_self。但根据我在本地开发页面上的测试,这两个都不起作用--当页面直接在Chrome中打开时,而不是通过脚本或来自另一个页面的链接。但是,在Edge中工作!:)但是单独使用window.close()也可以工作--有一个确认。

'but the actual problem is that I'm a moron.' - hilarious - we have all been there brother!

“但真正的问题是我是个白痴。”--搞笑--兄弟,我们都有过这样的经历!

Anecdotal evidence and personal assurances don't make for a better answer. Besides, this script will be prevented from running wherever the page wasn't opened by another script.

传闻证据和个人保证并不能提供更好的答案。此外,此脚本将被阻止运行的页面没有被另一个脚本打开。

Scripts may close only the windows that were opened by them. is what I get doing either of those. In Chrome console.

脚本只能关闭它们打开的窗口。这就是我在做这两件事时得到的。在Chrome控制台中。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com