How to pass username from one page to another

73 views
Skip to first unread message

Pgs Ss

unread,
May 20, 2023, 8:16:20 AM5/20/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 want to pass username from the first page (Index.html) to the other (meals.html) and display it there.
These are my files (3 only)(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>

Pgs Ss

unread,
May 20, 2023, 11:58:28 AM5/20/23
to Google Apps Script Community
Ok I found it:
sessionStorage.setItem('username', username.value);
sessionStorage.getItem('username');

Reply all
Reply to author
Forward
0 new messages