0

First, this is a google-app-script issue... I can't seem to capture the second (or subsequent) parameters within the HTML page (i.e. "item" in this example)... I've seen many examples using "location.search" and "window.location.search", but none of these seem to work. Could it possibly be as simple as "location.search" is not the correct usage?

Example

Code.gs

var myParam;
/**
 * Get the URL for the Google Apps Script running as a WebApp.
 */

function getScriptUrl() {
 var url = ScriptApp.getService().getUrl();
 return url;
}
/**
 * Get "home page", or a requested page.
 * Expects a 'page' parameter in querystring.
 *
 * @param {event} e Event passed to doGet, with querystring
 * @returns {String/html} Html to be served
 */
function doGet(e) {
  //Logger.log( Utilities.jsonStringify(e) );
  Logger.log(e.parameter.page);
  var pgToLoad = e.parameter.page;

  if (!e.parameter.page) {
    Logger.log('!e.parameter.page')
    // When no specific page requested, return "home page"
    // return HtmlService.createTemplateFromFile('my1').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
    return HtmlService.createTemplateFromFile('my1').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  }
  Logger.log('there is something for the page');
  // else, use page parameter to pick an html file from the script
  // return HtmlService.createTemplateFromFile(pgToLoad).evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return HtmlService.createTemplateFromFile(pgToLoad).evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

I have multiple HTML files, but they are basically the same as my1.html below... my1.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>Source = my1.html</h1>
    <p id=myParam>Placeholder</p>
    <?var url = getScriptUrl();?><a href='<?=url?>?page=my2&item=1-234'> <input type='button' name='button' value='my2.html'></a>
    <?var url = getScriptUrl();?><a href='<?=url?>?page=my3&item=1-345'> <input type='button' name='button' value='my3.html'></a>
  </body>
</html>
<script>
function getParam(sname)
{
  var params = location.search;
  var sval = "";
  params = params.split("&");
  // split param and value into individual pieces
  for (var i=0; i<params.length; i++)
  {
    temp = params[i].split("=");
    if ( temp[0] == sname ) { sval = temp[1]; }
  }
  return sval;
}
function changeItem() {
  var param = getParam("item");
  var myItem = "Item:-"+param+"-";
  document.getElementById("myParam").innerHTML = myItem;
}
window.onload = changeItem;
</script>
2
  • Is the changeItem function running without the parenthesis? window.onload = changeItem();. Maybe try window.location.href? Get the entire URL first. Commented Feb 11, 2016 at 2:49
  • @SandyGood - Yes, the changeItem is running - no parenthesis when using assignment '='... I tried window.location.href, I'm not getting ANYTHING. Commented Feb 12, 2016 at 14:48

2 Answers 2

1

I think I know what you want to do. It looks like you are getting the search string parameters from the doGet(e) function on the server side, then you are trying to get the same search string parameters again on the "client side" from the onload function? If this is the case, I would abandon trying to get the search string parameters from the client side.

You could store the search string parameters in the browsers sessionStorage:

window.sessionStorage.setItem("searchStringOne","Value One");

and to retrieve:

var valueOne = window.sessionStorage.getItem("searchStringOne");

Session Storage Information

Sign up to request clarification or add additional context in comments.

1 Comment

You were correct, and your solution provide me with an alternative... Thank you!
0

Here's an example to show how to get the query string parameters to serve different html templates using html-service of app script :

function doGet(e) {
   Logger.log( Utilities.jsonStringify(e) );

  // Load a home page when no parameter is specified

  if (!e.parameter.redirect) {
  var template = HtmlService.createTemplateFromFile('home');

  var htmlOutput =    template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setTitle('Home');
  return htmlOutput;
  }

  //get the page from parameter and load it  

 else{
 var template=HtmlService.createTemplateFromFile(e.parameter['redirect']);

 var htmlOutput = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setTitle('Other Page');

 return htmlOutput;
 }

}

function getScriptUrl() {

  var url = ScriptApp.getService().getUrl();

  return url;
}

then HTML will look like this :

home.html

        <?var url = getScriptUrl();?>

        You are on Home Page.

        <a href='<?=url?>?redirect=pagex'>Goto PageX</a>



        <a href='<?=url?>?redirect=pagey'>Goto PageY</a>

pagex.html

You are on PageX

pagey.html

You are on PageY 

Hope this helps!

3 Comments

I was trying to get the second parameter "item" in page=my2&item=1-234' - the code works, and redirects to the other pages, but I want the destination to retrieve the second parameter "item"...
Can you elaborate on "I want the destination to retrieve the second parameter 'item' " ? Can you put in steps like how and what you want to achieve.. I mean right from beginning when the first or home page loads and then step wise what you want
I am simply trying to pass parameters from one page to another... If you look in my1.html, I am passing two parameters "page=my2" and "item=1-234", however when I evaluate the window.location.search it is blank, not "?page=my1&item=2-123". I am being told this has something to do with the way google is providing the content. something to do with iFrame and possibly related to node js...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.