Google apps script location.reload in web app
Asked Answered
E

2

6

I am using a GAS web app that needs to refresh its contents when a user takes certain actions (like clicking on a particular div).

On the clientside, I have this script that gets called from onclick

google.script.run.withSuccessHandler(refreshCallback).myServersideFunction();

function refreshCallback(roomsPlus) {
    var statusDiv = document.getElementById("status");
    statusDiv.innerHTML = "Reloading...";
    window.location.reload(true);
};

The status div changes to "Reloading..." so I know the callback gets called,but then it never reloads the page. Does Caja or google apps script disable page refreshing? Is there another way to refresh the page?

Euridice answered 3/2, 2013 at 16:14 Comment(0)
O
7

All you need to do is use:

window.open("https://script.google.com/macros/s/web_app_ID/dev",'_top');

My original answer below works, but is un-needed:

There is a programmatic "Work Around" to reload/refresh the page in an Apps Script Web App. It is based on the capability of an html link tag to reload the page, and the ability of JavaScript to invoke the click event on that tag. You can hide the link if you want.

  • Add a link tag to your HTML
  • Hide the link tag if you wish
  • Add client side code in a script tag to invoke the click event on the link tag
  • Run the code however you want

Test Button:

<button onmouseup="reloadTheWebApp()">Trigger the Link Tag</button>

Link:

<a id="testLink" style="display:none" 
   href="https://script.google.com/macros/s/script_ID/exec">Reload the Web App</a>

Script:

<script>

  window.reloadTheWebApp = function() {
    console.log('reloadTheWebApp ran');

    var linkTag = document.getElementById('testLink');

    linkTag.click();
  }

</script>

It may be preferable to implement a simple page navigation system:

Page Navigation Web App - Apps Script

Oslo answered 11/12, 2017 at 14:15 Comment(3)
I've implemented this solution and it works perfectly if I test it through it's exec link, but if it's embedded into a Google Pages site, it does not get to the reload part. Why is that so?Melia
Sorry, I don't know.Oslo
@AlanWells Thank you very much for providing this solution, as Benjamain mentioned, it works all good but when I add it to Google sites, the reload part doesn't work .Dizon
L
4

EDIT

Caja was removed July 16, 2016 from Web App HtmlService.

Native Sandbox Mode

An Update to HTML Service

End of edit

Unfortunately the Caja sanitization engine used by HtmlService restricts access to the window object, preventing you from programatically reloading the page. This is a known restriction of the service and we are working to better document it.

Laverty answered 1/3, 2013 at 23:17 Comment(2)
So, how does one solve this problem? Having the GAS page refresh?Eloiseelon
Caja is now no longer used in Apps Script Web Apps.Oslo

© 2022 - 2024 — McMap. All rights reserved.