Search code examples
javascriptjqueryfancyboxwindow.closed

Need to Close Current Window on fancybox Button Click


I need it for age verification, When i click on under21 Age button of fancybox- my Current window should be closed.

my code is like this:

<div style="display:none;">
<div id="age-popup">
    <div style="margin: 0 auto;text-align: center;">
        <h2>Welcome!</h2>
        <h3>Please Verify Your Age to Enter.</h3>
        <input type="button" name="age" onclick="return overage();" value="21 & Over" />
        <input type="button" id="WarringMsg" name="WarringMsg" onclick="close_window()" value="Under 21" />
    </div><br>
    <p>Our product is not appropriates for individuals under 18 and window will be close automatically.</p>
</div>

function overage()
{
      user = "verifieduser";
      setCookie("username", user, 30);
     jQuery.fancybox.close();   
}
function close_window() {
   // window.open("about:blank","_self");
   // window.close();
     window.open('','_self');
     self.close();
}

window.close is not working in Chrome and FF.


Solution

  • Ordinary javascript cannot close windows willy-nilly. This is a security feature, introduced a while ago, to stop various malicious exploits and annoyances.

    From the latest working spec for window.close():

    The close() method on Window objects should, if all the following conditions are met, close the browsing context A:

    The corresponding browsing context A is script-closable. The browsing context of the incumbent script is familiar with the browsing context A. The browsing context of the incumbent script is allowed to navigate the browsing context A. A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a browsing context whose session history contains only one Document. This means, with one small exception, javascript must not be allowed to close a window that was not opened by that same javascript.

    Chrome allows that exception -- which it doesn't apply to userscripts -- however Firefox does not. The Firefox implementation flat out states:

    This method is only allowed to be called for windows that were opened by a script using the window.open method. If you try to use window.close from a Greasemonkey / Tampermonkey / userscript you will get: Firefox: The error message, "Scripts may not close windows that were not opened by script." Chrome: just silently fails.

    The long-term solution:

    The best way to deal with this is to make a Chrome extension and/or Firefox add-on instead. These can reliably close the current window.

    However, since the security risks, posed by window.close, are much less for a Greasemonkey/Tampermonkey script; Greasemonkey and Tampermonkey could reasonably provide this functionality in their API (essentially packaging the extension work for you). Consider making a feature request.

    The hacky workarounds:

    Chrome is currently vulnerable to the "self redirection" exploit. So code like this will currently work in Tampermonkey scripts:

    open(location, '_self').close(); This is buggy behavior, IMO, and is liable to be blocked by future releases of Chrome, so use this hack with that in mind.

    Firefox is secure against that exploit. So, the only javascript way is to cripple the security settings, one browser at a time.

    You can open up about:config and set allow_scripts_to_close_windows to true.

    If your script is for personal use, go ahead and do that. If you ask anyone else to turn that setting on, they would be smart, and justified, to decline with prejudice.

    There currently is no equivalent setting for Chrome. This may work for some browsers : HTML:

    <input type="button" name="Quit" id="Quit" value="Quit" onclick="return quitBox('quit');" />
    JavaScript:
    
    function quitBox(cmd)
    {   
        if (cmd=='quit')
        {
            open(location, '_self').close();
        }   
        return false;   
    }
    

    Try this test page: (Now tested in Chrome 40 and Firefox 35)

    http://browserstrangeness.bitbucket.org/window_close_tester.htm