Reputation: 338
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?
Upvotes: 16
Views: 40576
Reputation: 61
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>`
);
Upvotes: 0
Reputation: 1944
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.
Upvotes: 0
Reputation: 508
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.
Upvotes: 20
Reputation: 7858
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.)
Upvotes: 10