Search code examples
javascriptjquerygoogle-chromebrowser-tab

Javascript Cannot Close Current Tab After Chrome 36.0.1985.125 Update


Currently I have an application where a user hits a clicks on a URL and goes to my form. The user then enters his information into the form field, which is then sent through jquery ajax to a PHP script which enters it into the database. Upon success callback, it would alert the user that they had been registered and closes the current browser tab. (Let's just say I need the closing the browser tab behaviour to persist).

$.ajax({
    type: "POST",
    url: 'goToPHP',
    data: data,
    success: function(data){
       alert('You Have Been Registered Successfully');
       open(location, '_self').close();
    },
    error: function(data){
    }
});

I understand that most modern browsers (Chrome included) are limiting the ability for javascript to only close tabs/windows it created for security reasons. Temporarily I had used open(location, '_self').close(); to get around the issue, but alas, it seems chrome's most recent update prevents you from doing this as well (Prompts you with a warning: 'Scripts may close only the windows that were opened by it'.)

Is there a way around this? I'm not talking about something along the lines of:

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

But something that will work all the time on Chrome (e.g. changing a Chrome setting to allow scripts to close tabs/windows (similar to how this could be done through about:config in Firefox) or a way of restructuring how the user hits the form, so that the window object is available in javascript so i can call windowObject.close(); ) .

Thanks in Advance!


Solution

  • 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 top-level browsing context whose session history contains only one Document.

    http://www.w3.org/html/wg/drafts/html/master/browsers.html#dom-window-close

    In effect this means that, in common usage, you can only close windows/tabs that you have created in JavaScript. However, it is also possible to close a window or tab (but not a frame or other nested browsing context) as long as that window or tab has no history. Usually, this is a highly unreliable feature to make use of as you generally cannot realistically expect your user to have opened no documents in their browsing context before opening the one that you are trying to close; however, there are certain cases where you can safely assume that the page is in a context with no history, but generally these cases would fall under the 'opened by script' categorization as well.

    Additionally, if you use a link to '#' for JavaScript purposes or any other sort of bookmark linking within your page, that will populate the session history and render the context unclosable unless you use JavaScript to prevent the hyperlink event from being executed. For purposes of JavaScript, it is probably better practice to just use javascript:void(0) if this is an issue.

    In your case, the only way you can guarantee that you can close the tab/window is if you can guarantee that your form is being opened in a browsing context with no history or if you can open the form page via JavaScript. Whether or not those limitations are reasonable to work around depends on the specific structure/implementation of your website.