4

I created a form using the app script. In the html file I have the below;

<form name="Subscribe-to-Central" id="Subscribe-to-Central" action="https://script.google.com/macros/s/key/exec" method="POST" onsubmit="myFunction()">  

Inputs ..

</form>

<script>
function myFunction() {
  alert("Successfully subscribed. Please press okay to return to the home page");
  window.open("URL", "_top");
}
</script>

The form is working good at which sending the date to the attached sheet and also redirecting to the "URL" after submit but the problem is when I tried to embed the form in the new google sites, it still sends the data to the sheet but no more redirect and it gives the following error "script.googleusercontent.com refused to connect."

PS: Please note that I face this problem only with new google sites. I tried embedding the same script in classic google sites and it worked just fine

Screenshot of the error enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    Do you get an error in the console when you get the `script.googleusercontent.com refused to connect` error? If so please paste it here, I have a feeling this is due to the [same-origin policy](https://stackoverflow.com/a/58300375/11551468). – Rafa Guillermo Jan 20 '20 at 15:25
  • Thank you for your interest Rafa. No only that error is displayed. I added a screenshot of the error to the question. I use a domain like "https://sites.google.com/view/Sitename" and it is published with specific people so far – Mahmoud Bayoumi Jan 20 '20 at 15:47
  • Can you please press `F12` and clicking the 'console' tab before attempting to reload again? If anything is displayed please paste it here. – Rafa Guillermo Jan 20 '20 at 15:53
  • I took another screenshot, please see in the question – Mahmoud Bayoumi Jan 20 '20 at 17:39

2 Answers2

4

Answer:

Unfortunately, due to changes in how New Sites work compared to Classic sites, it is no longer possible to complete a redirect in a New Site.

More Information:

As you can see in the console, you get the following error when attempting to navigate the top-level window from JavaScript:

Unsafe JavaScript attempt to initiate navigation for frame with origin https://sites.google.com from frame with URL https://<id>.script.googleusercontent.com/userCodeAppPanel. The frame attempting navigation of the top-level window is sandboxed, but the flag of allow-top-navigation or allow-top-navigation-by-user-activation is not set.

and:

Refused to display <URL> in a frame because it set X-Frame-Options to sameorigin.

It is possible to set the X-Frame-Options for the embedded Google Apps Script page using the .setXFrameOptionsMode() method of HtmlService and using the XFrameOptionsMode Enumerator as shown here:

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('index')
                    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

Unfortunately, the Sandbox needs the allow-top-navegation or allow-top-navegation-by-user flag to be able to redirect from a Sandbox. As per hte documentation, the only Sandbox modes available from HtmlService are the following Enmerators:

  • IFRAME: A sandbox mode that uses iframe sandboxing instead of the Caja sandbox technology used by the EMULATED and NATIVE modes. This mode is the default for new scripts as of November 12, 2015 and for all scripts as of July 6, 2016.

  • NATIVE: A sandbox mode that is built on top of ECMAScript 5 strict mode. A sandbox mode built on top of ECMAScript 5 strict mode. This mode was sunset as of July 6, 2016. All scripts now use IFRAME mode.

  • EMULATED: A legacy sandbox mode that emulates ECMAScript 5 strict mode using only the features available in ECMAScript 3. This mode was the default prior to February 2014. Actually deprecated, All scripts attempting use EMULATED will now use IFRAME instead.

The setting of flags for the Sandboxed embed isn't possible to do from within the New Sites interface either, so adding the required navigation allow flag can't be done from Sites end either.

What you can do:

There isn't anything here that can be done as long as you are working with New Sites. As you have already pointed out, however, Classic Sites do allow this if this is a suitable workaround.

References:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • Yes, I tried many things but no luck with new sites. I have an idea of creating another .html file in the app script and display that inside the frame itself once the form is submitted and inside that html page I can add some links and/or information. Would you please guide me how to return the form to an html file after submit and thank you very much Rafa for detailed explanation and support – Mahmoud Bayoumi Jan 21 '20 at 10:39
  • Check out [`google.script.run`](https://developers.google.com/apps-script/guides/html/reference/run) for running Apps Script functions from the HTML files, and make a conditional `if` statement which returns a different `HtmlService.createHtmlOutputFromFile('html-file-name')` depending on the conditions you need. If you need any more guidance - please feel free to ask a new question! – Rafa Guillermo Jan 21 '20 at 10:42
-1

solution

To resolve the issue you have to set the target of HTML page and make sure you use domain name if you are setting up page links dynamically. check the attached pic for details

Community
  • 1
  • 1
  • 2
    This question already has an accepted answer. If you feel the accepted answer is incorrect, then you should include sufficient evidence to show this. The accepted answer here includes a very detailed explanation of the answer, including references, and would be a good model to follow for any alternative answers. – Harlow Burgess May 26 '20 at 21:19