1

I have a doGet function in my google apps script (attached to a google doc) which is published as a web app and I want the user to be redirected to another web page. Importantly, I want the URL of the page they are redirected to be displayed in the address bar, and the title of the page they are redirected to be the title of the tab (in Chrome).

I've tried using a meta refresh tag, and setting the window.location.href. Both of these redirect correctly but they show the URL of the address app in the address bar, not the URL of the page the user is redirected to.

The below script, attached to a Google Doc, illustrates the problem.

function doGet(request) {
  var drive = DriveApp;
  var docs = DocumentApp;
  var Id = docs.getActiveDocument().getId();
  var document = docs.openById(drive.getFileById(Id).makeCopy().getId());
  document.setName("Test doc 2");
  var URL = document.getUrl();
  return HtmlService.createHtmlOutput('<meta http-equiv="refresh" content="0; url=' + URL + '" />')
}

Publishing the script as a web app and then visiting the URL redirects you to the newly created document, but it is the URL of the script that displays in the address bar.

See this Google Doc for an example: https://docs.google.com/document/d/1HpBkNGGGjKj3W6QXThtGdniSO_UTANo8LcqmgZowdTQ/edit

Gabriel
  • 13
  • 3
  • Do you mean that you want the WebApp to redirect to a page that is not within the webapp or within the webapp? Perhaps you can provide a bit more code so that we can confirm the behavior ourselves ie [mcve]. – Cooper Jun 20 '19 at 14:39
  • @Cooper Thanks for responding. I want to redirect to a new document that is created by the script. I've augmented the code sample to illustrate this. – Gabriel Jun 20 '19 at 15:11

1 Answers1

2

Since your html is loaded in a inner iframe, You should use

window.top.location = url

to load in the top frame.

TheMaster
  • 45,448
  • 6
  • 62
  • 85