Automatically Redirecting to a Page
Asked Answered
C

4

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?

Catchascatchcan answered 3/7, 2012 at 16:50 Comment(2)
I don't think this is possible now, there is an enhancement request about itPiassava
@Sergeinsas: The referred issue has status Won't Fix (Infeasible)Salyers
T
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.)

Tomkins answered 3/7, 2012 at 17:11 Comment(5)
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.Catchascatchcan
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.Tomkins
This is not working anymore. I have opened an issue. Star it to keep track of updates. @CoreyG please take a look.Horsemint
@HenriqueG.Abreu: The issue has status "Won't fix (intended behaviour)Salyers
This answer is no longer valid; see the answer from David Lopez for one that works.Babbling
L
20

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.

Lacee answered 30/12, 2017 at 2:5 Comment(3)
this gives me an error. "Unsafe attempt to initiate navigation for frame with origin '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 chromestatus.com/feature/5629582019395584."Gyron
Not working anymoreMatchwood
Need to make sure that the script contains <base target="_top"> because it specifies a default target for all hyperlinks and forms on a page. top Opens the link in the full body of the windowFromenty
T
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.)

Tomkins answered 3/7, 2012 at 17:11 Comment(5)
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.Catchascatchcan
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.Tomkins
This is not working anymore. I have opened an issue. Star it to keep track of updates. @CoreyG please take a look.Horsemint
@HenriqueG.Abreu: The issue has status "Won't fix (intended behaviour)Salyers
This answer is no longer valid; see the answer from David Lopez for one that works.Babbling
S
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

Sundried answered 16/8, 2023 at 5:51 Comment(0)
S
0

Updating from 2024, the window.top.locations doesn`t work. But is it possible using this:

  const url = `https://PUT-URL-HERE.com`;
  return HtmlService.createHtmlOutput(
    `<script>window.open('${url}', '_self');</script>`
  );
Soerabaja answered 25/7 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.