google-apps-scriptgoogle-sites-2016

New Google Sites and URL query strings


I'm required to build a website using the new google sites. I want to pass url query strings between pages. I'm exploring the class google.script.url a client side script.

How can I get this to work?

google.script.url.getLocation(function(location) {
   console.log(location);
});

In my browser console I get the following error

Uncaught ReferenceError: google is not defined

What am I missing? How to reference google.script.url?

Thank you


Solution

  • Ok, I think you are mixing concepts because your tags say "google-sites-2016" and "google-apps-script", which are different Google services.

    For Google sites, as it says HERE, you are not using the right service.

    If you need code editing the new Google Sites is the wrong platform for you and there are many others that will do this for you.

    Now, if you want to use Apps Script as you may want for what I understood from your question. Follow these steps:

    1) Go to this link

    2) Click "New Project"

    3) Paste this code in the script:

    function doGet() {
      return HtmlService.createHtmlOutputFromFile('Index');
    }
    
    function getURL() {
      return ScriptApp.getService().getUrl();
    }
    

    4) Go to File -> New -> HTML File

    5) Paste this HTML :

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <script>
          function onSuccess(url){
           console.log(url);
         }
         google.script.run.withSuccessHandler(onSuccess).getURL();
      </script>
      <body>
        Hello world!
      </body>
    </html>
    

    6) Go to Publish -> Deploy as web app -> Deploy

    7) You will get a link. Go to that link

    Docs

    You can find more info about it here: