window.close and self.close do not close the window in Chrome

后端 未结 16 1847
孤城傲影
孤城傲影 2020-11-21 05:48

The issue is that when I invoke window.close() or self.close() it doesn\'t close the window. Now there seems to be a belief that in Chrome you can\

16条回答
  •  后悔当初
    2020-11-21 06:12

    Chrome Fixed the security issues on version 36.0.1985.125

    Chrome 36.0.1985.125 WEDNESDAY, JULY 16, 2014 Release note

    From my observation, this update fixed the issue on using window.close() to close the popup window. You will see this in the console when it fail, "Scripts may close only the windows that were opened by it.". That means The hacky workarounds (Brock Adams's answer) may not work in the latest release.

    So, in the previous Chrome released builds, the below code block may worked but not with this update.

    window.open('', '_self', '');
    window.close();
    

    For this update, you have to update your code accordingly to close the popup window. One of the solution is to grab the popup window id and use

    chrome.windows.remove(integer windowId, function callback)
    

    method to remove it. Chrome extension windows API can be found at chrome.windows.

    Actually my chrome extension MarkView was facing this issue and I had to update my code to make it work for this Chrome Update. By the way, MarkView is tool to read and write Awesome Markdown Files, it provides features including Content Outline, Sortable Tables and code block syntax highlight with line number.

    I also created this post, any comments are welcome.

提交回复
热议问题