0

I made successful code to redirect to another page using web script just in case I add query string manually. like this [main url]?Page=Partial

the doGet code

  function doGet(e) {
    var template;
    var pageName = e.parameter.page === undefined ? "Index" : e.parameter.page;  
    if (pageName === "Index") {
      template = HtmlService.createTemplateFromFile(pageName);
    }else{
      var url = ScriptApp.getService().getUrl();
      var getPageUrl = url + "?page=" + pageName;
      template = HtmlService.createTemplateFromFile(pageName);
      template.url = getPageUrl;
    }

    return template.evaluate();
  }

but when I try to href using anchor element It never work

  function href(pageName){
      var url = ScriptApp.getService().getUrl();
      var fullPageUrl = url + "?page=" + pageName;
      return fullPageUrl;
  }

then I use this in Index.html like this

 <a href="<?= href('Partial') ?>"></a>

notes:
if I do inspect for the page and click the href link it works well, but clicking directedly never work and come back with a page with error Forbidden Error 403

clicking href link content, when inspect page, or changing url manually only the successful ways to redirect to another page. and it never works by clicking directly on the href element and it comes back instead with error Forbidden Error 403

Rubén
  • 34,714
  • 9
  • 70
  • 166
devoloper
  • 39
  • 6
  • I have to apologize for my poor English skill. Unfortunately, from your question, I cannot understand the flow for replicating your issue of `error Forbidden Error 403`. Can I ask you about the detailed flow for correctly replicating your current issue? – Tanaike Apr 02 '22 at 02:45
  • Sure @Tanaike, and thank you for trying to help me. I get that error just in case I click href element directedly. although, when I right click and inspect the page and click or copy href content it redirect as needed very well. – devoloper Apr 02 '22 at 02:51
  • When you access a spreadsheet from a webapp you do not open up a user interface so the idea of moving to aother page makes no sense. You can read or write to any page you wish but there is no user interface opened for you to view. – Cooper Apr 02 '22 at 04:27
  • Please add a [mcve] (the code of the Index.html page should be complete, not only an anchor tag) – Rubén Apr 02 '22 at 04:44
  • 1
    Possible duplicate of [Linking to another HTML page in Google Apps Script](https://stackoverflow.com/q/15668119/1595451) – Rubén Apr 02 '22 at 04:45
  • 1
    Thank you for replying. From your reply of `I get that error just in case I click href element directedly. although, when I right click and inspect the page and click or copy href content it redirect as needed very well.`, please modify `` to `sample` and test it again. I thought that by `target="_top"`, your current issue might be removed. – Tanaike Apr 02 '22 at 04:54
  • When I saw [the thread](https://stackoverflow.com/questions/71714405/how-to-redirect-to-another-page-using-google-web-script?noredirect=1#comment126739859_71714405) provided by @Rubén, I thought that using `target="_top"` has already been mentioned. So I flagged your question as the duplicated question. – Tanaike Apr 02 '22 at 04:58
  • @Tanaike thank you it's working now by adding `target="_top"` – devoloper Apr 02 '22 at 09:01
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Apr 02 '22 at 12:21

0 Answers0