This question already has an answer here:
这个问题在这里已有答案:
- How to close current tab in a browser window? 11 answers
如何关闭浏览器窗口中的当前选项卡? 11个答案
In a page I have a 'Close' button that when clicked has to close the current browser tab (the tab in which is the page that contain this close button). So I wrote something like this:
在页面中,我有一个“关闭”按钮,单击该按钮时,必须关闭当前浏览器选项卡(其中包含此关闭按钮的页面的选项卡)。所以我写了这样的话:
<button type="button" class="btn btn-default" style="color: #0F8BB0;" onclick="window.close();">
Close
</button>
But it doesn't work and the current browser tab is not closed. Why? What am I missing? How can I try to solve this issue?
但它不起作用,并且当前的浏览器选项卡未关闭。为什么?我错过了什么?我该如何尝试解决这个问题?
1 个解决方案
#1
2
You are doing it correctly, however browsers prevent you from closing a window if your javascript didn't create it.
你正确地做到了,但是如果你的javascript没有创建它,浏览器会阻止你关闭一个窗口。
So basically you can only close a window if you opend that window through javascript.
所以基本上只有在通过javascript打开窗口时才能关闭窗口。
Also see: window.close and self.close do not close the window in Chrome
另请参阅:window.close和self.close不要关闭Chrome中的窗口
#1
2
You are doing it correctly, however browsers prevent you from closing a window if your javascript didn't create it.
你正确地做到了,但是如果你的javascript没有创建它,浏览器会阻止你关闭一个窗口。
So basically you can only close a window if you opend that window through javascript.
所以基本上只有在通过javascript打开窗口时才能关闭窗口。
Also see: window.close and self.close do not close the window in Chrome
另请参阅:window.close和self.close不要关闭Chrome中的窗口