Redirect with javascript insted clicking in Apps Script

2,019 views
Skip to first unread message

Pgs Ss

unread,
May 21, 2023, 12:24:29 PM5/21/23
to Google Apps Script Community
I have an Apps Script which when opens, loads index.html.
I fill in the 'username'.
I click on the 'Submit and open Meals'.

I will change <a href="?page=meals">Submit and open Meals</a> to <a>Submit and open Meals</a>  
(thus I'ii remove href)

I want to to it with javascript. That is, instead of clicking the link, I want something like 'window.locate(url)', which does not work in Apps Script.
Any help is welcome.

These are my files (3 only, easy to try)(I see no code quoting, container or however it's called)

Code.gs:
//@return Base Url
function getUrl() {
  return ScriptApp.getService().getUrl()
}
//@return Html page raw content string
function getHtml(hash) {
  return HtmlService.createHtmlOutputFromFile(hash).getContent()
}

//@return provided page in the urlquery '?page=[PAGEID]' or main index page
function doGet(e) {
  var page = e.parameter.page
  return HtmlService.createTemplateFromFile(page || 'index')  
    .evaluate();
} index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <!--
      The <base> tag specifies the base URL and/or target for all relative URLs in a document.
      The <base> tag must have either an href or a target attribute present, or both.
      There can only be one single <base> element in a document, and it must be inside the <head> element.
    -->
    <title>Open another Page App</title>  
   

    <style>
      h1 {
        text-align: center;
        margin: 2px;
        text-transform: uppercase;
        background-color: green;
      }
      span:hover,
      a:hover {
        background-color: yellowgreen;
      }
      body {
        background-color: brown;
        color: white;
        font-size: 2em;
      }
      a:visited {
        color: white;
      }
    </style>
  </head>
 
   <body>
    <h1 id="type">Open Another Page App Demo</h1>
    <div id="main"><div>Loading...</div>  
     

       <label for="username">Username:</label>
      <input type="text" id="username" name="fusername">

      <ul><!-- here are the real names of html pages -->
        <li><a href="?page=meals">Submit and open Meals</a></li>        
      </ul>
     
    </div>
   
       
<script>
  //Change base url (href of tag named 'base')
  google.script.run
    .withSuccessHandler(url => {    
      $('base').attr('href', url)
    })
    .getUrl()
</script>

  </body>
</html>
meals.html
<!DOCTYPE html>
<html>
  <head>
    <h3>This is Meals</h3>
    <p>Hello World!</p>
    <div>Your username is: </div>
  <head>
  <body>
  </body>  
</html>

Reply all
Reply to author
Forward
0 new messages