Chrome和Firefox的window.close()方法的替代方法

问题描述:

当我单击同一页面上的关闭按钮时,我想关闭我的窗口/标签.

I want to close my window/tab when the close button is clicked on the same page.

我使用了window.close()方法.它适用于IE版本,但不适用于Firefox和Chrome.

I used window.close() method. It works perfectly for IE versions, but it doesn't work for Firefox and Chrome.

我进行了很多搜索以寻求解决方案.请给我一个演示解决方案.

I searched a lot for a solution. Please give me a demo solution.

根据 MDN

仅允许使用window.open()方法由脚本打开的窗口调用

[Window.close()].如果未通过脚本打开窗口,则JavaScript控制台中会出现以下错误:脚本可能无法关闭不是由脚本打开的窗口.

[Window.close()] is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

因此,要使用此方法,必须使用window.open()方法才能通过window.close()将其关闭.

So in order to use this method, you have to use the window.open() method to be able to close it via window.close().