1

I have a web app in Google Apps Script and I need that when I have click in a Button, the browser tab closes. I tried with different solutions but it does not work as I require it.

This is my HTML code.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>Demo Close tab</h1>
    <p>When you click the button, you must close this tab.</p>
    <button>Close</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script>
    window.onload = function() {
      $("button").click(function() {
        // window.close();
        // window.top.close();
        // window.open("","_top","").close();
      });
    }
    </script>
  </body>
</html>

And this is my GS code

function doGet() {
  var template = HtmlService.createTemplateFromFile("Index");
  
  return template.evaluate()
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .addMetaTag("viewport", "width=device-width, initial-scale=1")
  .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
  .setTitle("Demo Google Analytics");
}
  • Take a look at [this question](https://stackoverflow.com/questions/14373625/close-current-tab). – Cooper Oct 26 '17 at 23:26

3 Answers3

0

As stated in the docs HTML Service: Restrictions:

The setSandboxMode method now has no effect when called.

Your Google Apps Script runs in an iframe by default, regardless of any attempts to change the sandbox mode. I strongly suspect the sandbox settings will prevent you accessing the parent window, but you might try window.parent.window.

skylize
  • 1,401
  • 1
  • 9
  • 21
0

According to Javascript Documention: Window.close()

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

Cooper
  • 59,616
  • 6
  • 23
  • 54
0

Web Apps are usually standalone script projects that, as per the documentation on restrictions applied in the IFRAME sandbox mode (which is, as of 2021, the only mode available), has the following sandbox flags (corresponding to the sandbox attribute) set on the HTMLIFrameElement Web Apps are served in:

Flag
allow-same-origin
allow-forms
allow-scripts
allow-popups
allow-downloads
allow-modals
allow-popups-to-escape-sandbox
allow-top-navigation-by-user-activation

Note the last flag - this is the reason for being able to initiate navigation when a user clicks on an element but not when calling methods like window.top.close(), window.location.reload(), or window.location.replace(<url>).


This seemed to not be strictly enforced for bound script projects up until September 2021, but it is now. As of now, there are several open issues on the Issue Tracker reporting that the auto-closing Web App windows stopped working - please follow them if you want to get updates on whether or not Google will reconsider enforcing the flag: