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


the issue when invoke window.close() or self.close() doesn't close window. there seems belief in chrome can't close script window not script created. patently false regardless supposed still it, if requires pop alert confirm. these not happening.

so have real, functional , proven method of closing window using javascript:window.close() or javascript:self.close() expected , happens fine in every browser not chrome based? suggestions appreciated , looking javascript specific solution, nothing jquery or third party implementation.

update: while of has been suggested has serious limitations , usability issues, latest suggestion (specific tampermonkey) using // @grant window.close in script header trick on tabs can't handle close method. while not entirely ideal , doesn't generalized every case, solution in case.

ordinary javascript cannot close windows willy-nilly. security feature, introduced while ago, stop various malicious exploits , annoyances.

from the latest working spec window.close():

the close() method on window objects should, if following conditions met, close browsing context a:

  • the corresponding browsing context a script-closable.
  • the browsing context of incumbent script familiar browsing context a.
  • the browsing context of incumbent script allowed navigate browsing context a.

a browsing context script-closable if auxiliary browsing context was created script (as opposed action of user), or if browsing context session history contains 1 document.

this means, 1 small exception, javascript must not allowed close window not opened same javascript.

chrome allows exception -- doesn't apply userscripts -- firefox not. the firefox implementation flat out states:

this method allowed called windows opened script using window.open method.


if try use window.close greasemonkey / tampermonkey / userscript get:
firefox: error message, "scripts may not close windows not opened script."
chrome: silently fails.



the long-term solution:

the best way deal make chrome extension and/or firefox add-on instead. these can reliably close current window.

however, since security risks, posed window.close, less greasemonkey/tampermonkey script; greasemonkey , tampermonkey reasonably provide functionality in api (essentially packaging extension work you).
consider making feature request.



the hacky workarounds:

chrome is currently vulnerable "self redirection" exploit. code used work in general:

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

this buggy behavior, imo, , (as of april 2015) blocked. still work injected code only if tab freshly opened , has no pages in browsing history. it's useful in small set of circumstances.

however, a variation still works on chrome (v43 & v44) plus tampermonkey (v3.11 or later). use explicit @grant , plain window.close(). eg:

// ==userscript== // @name        window.close demo // @include     http://your_server.com/your_path/* // @grant       gm_addstyle // ==/userscript==  settimeout (window.close, 5000); 

thanks zanetu update. note not work if there 1 tab open. closes additional tabs.


firefox secure against exploit. so, javascript way cripple security settings, 1 browser @ time.

you can open about:config , set
allow_scripts_to_close_windows true.

if script personal use, go ahead , that. if ask else turn setting on, smart, , justified, decline prejudice.

there no equivalent setting chrome.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo