16

Inside a button click handler, I'm creating a new web page like so:

var page = SitesApp.getPageByUrl(url).createPageFromTemplate(title, name, template);

and I want to redirect the user automatically to that page.

I wasn't able to find much information, can this be done?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Robert Jakubowicz
  • 338
  • 1
  • 4
  • 11
  • 1
    I don't think this is possible now, there is an [enhancement request about it](http://code.google.com/p/google-apps-script-issues/issues/detail?id=1061) – Serge insas Jul 03 '12 at 17:09
  • @Sergeinsas: The referred issue has status `Won't Fix (Infeasible)` – Rubén Oct 24 '17 at 16:56

3 Answers3

18

Corey G's answer worked for me, but the problem is that the web page that I was redirecting was embedded into an iframe in the response of the GAS, so the real URL in the web browser was the script's one.

This is what actually worked for me:

function doGet() {
  return HtmlService.createHtmlOutput(
    "<script>window.top.location.href='https://example.com';</script>"
  );
}

This similar Q/A helped me to solve it.

Hope it helps.

David López
  • 488
  • 3
  • 8
  • 1
    this gives me an error. "Unsafe attempt to initiate navigation for frame with origin 'https://script.google.com' from frame with URL 'https://{id}-script.googleusercontent.com/userCodeAppPanel'. The frame attempting navigation of the top-level window is sandboxed with the 'allow-top-navigation-by-user-activation' flag, but has no user activation (aka gesture). See https://www.chromestatus.com/feature/5629582019395584." – A5H1Q Sep 23 '21 at 12:49
  • Not working anymore – Waqar Ahmad Nov 23 '21 at 07:21
  • Need to make sure that the script contains `````` because it specifies a default target for all hyperlinks and forms on a page. `top` Opens the link in the full body of the window – Maulik Pipaliya Joyy Mar 26 '22 at 13:23
10

This cannot be done in UiApp but it's doable in HtmlService:

function doGet() {
  return HtmlService.createHtmlOutput(
    "<form action='http://www.google.com' method='get' id='foo'></form>" + 
    "<script>document.getElementById('foo').submit();</script>");
}

This should be easier; please file a feature request in the issue tracker and we will see how we can make this more pleasant.

(Edit: To be clear, there's no way to do this from a UiApp callback; your entire app would have to be using HtmlService for this to work.)

Corey G
  • 7,754
  • 1
  • 28
  • 28
  • Thanks, unfortunately the widget started as a UiApp so for now I made it a 2 steps process by adding an anchor. I also voted for the feature in the link provided by @Serge-insas. – Robert Jakubowicz Jul 04 '12 at 16:01
  • If you still want to, it occurred to me that you could make this work in the following mildly convoluted way: 1. Change the button to an anchor 2. Have the anchor point to another script. 3. Have that other script do the processing and return the redirect via the HtmlService method. Depending on what info you need to pass along to the second script, this may or may not be doable for you. – Corey G Jul 05 '12 at 04:41
  • 2
    This is not working anymore. I have opened an [issue](http://code.google.com/p/google-apps-script-issues/issues/detail?id=1995). *Star* it to keep track of updates. @CoreyG please take a look. – Henrique G. Abreu Oct 26 '12 at 02:42
  • @HenriqueG.Abreu: The issue has status "Won't fix (intended behaviour) – Rubén Oct 24 '17 at 16:55
  • This answer is no longer valid; see the answer from David Lopez for one that works. – stevegt Jun 04 '18 at 05:00
0

This is what worked for me for url redirection:

function doGet(e) {
  // ... your code here ...

  // redirect to some page after the work is done
  const url = 'https://example.com/'
  return HtmlService.createHtmlOutput(
    `<script>window.location.replace("${url}");</script>`
  );
}

Be aware though that this also displays the Google warning banner at the top:

This application was created by another user, not by Google.

enter image description here

qaziqarta
  • 1,782
  • 1
  • 4
  • 11