0

i have a simple WebApp.

I wrote script in another file ( not directly in Index.html)

I set onClick attribute to ButtonA from Index.html successfully.

and tried to set another onClick attribute to ButtonB from JavaScript.html file . But it's not working :(

What went wrong or how to achieve this..?

WebApp Link

App Script Link

Below is the Code.gs file

function doGet() {
  var template = HtmlService.createTemplateFromFile('Index.html') ;
  var TodaysUrl = "https://google.com" ;
  template.TodaysUrl = TodaysUrl;
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent() ;
}

Below is the Index.html file

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">

    <style>
      .button {
        display : block ;
        text-align : center ;
        margin : 25px auto ;
        padding : 10px
      }
    </style>
  </head>

  <body>
      
    <button class="button" id="btn-a" >ButtonA Link from Index.html</button>

    <button class="button" id="btn-b" >ButtonB Link from JavaScript.html</button>

    <script>

      document.getElementById("btn-a").onclick = function () { window.open("<?= TodaysUrl ?>" )} ;

    </script>  

    <?!= include('JavaScript'); ?>

  </body>
</html>


Below is the JavaScript.html file

<script>

      document.getElementById("btn-b").onclick = function () { window.open("<?= TodaysUrl ?>" )} ;

</script>  

Thanks in Advance

3
  • Hello, the script inside the JavaScript.html file should place inside the Index.html file. Then it will work as you expect. Commented Sep 29, 2022 at 13:49
  • Something is parsing index.html, and converting <?= TodaysUrl ?> into a url. That thing is not parsing JavaScript.html. Commented Sep 29, 2022 at 13:49
  • Hi @James , then how to achieve this. I modified my code as TheWized said.. pls help Commented Sep 29, 2022 at 17:51

2 Answers 2

1

Your second button is not working because loaded in second time, and is not rendered by the "evaluate" function.

doGet > evaluate (define btnA) > includes JS file (which contains btnB, so won't render)

My suggestion would be to initialize your buttons with a custom function called after the DOM is loaded. Here an example with 2 differents URLs

Code.gs

function doGet() {
  var template = HtmlService.createTemplateFromFile('Index.html') ;
  // var TodaysUrl = "https://google.com" ;
  // template.TodaysUrl = TodaysUrl;
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent() ;
}

function initialize() {

  var obj = {};

  obj.url_a = "http://google.com";
  obj.url_b = "http://amazon.com";

  return obj;

}

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">

    <style>
      .button {
        display : block ;
        text-align : center ;
        margin : 25px auto ;
        padding : 10px
      }
    </style>
  </head>

  <body>
      
    <button class="button" id="btn-a" >ButtonA</button>
    <button class="button" id="btn-b" >ButtonB</button>

    <?!= include('JavaScript'); ?>

  </body>
</html>

Javascript.html

<script>

  document.addEventListener("DOMContentLoaded", function() {
    google.script.run.withSuccessHandler(return_initialize).initialize();
  });

  function return_initialize(obj) {
    document.getElementById("btn-a").onclick = function () { window.open(obj.url_a)} ;
    document.getElementById("btn-b").onclick = function () { window.open(obj.url_b)} ;
  }

</script>  
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass TodaysUrl to the template.

function doGet() {
  var TodaysUrl = "https://google.com" ;
  var template = HtmlService.createTemplateFromFile('Index') ;
  template.TodaysUrl = TodaysUrl;
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

Include your script in the body of the html rather than from another file.

7 Comments

Hi @TheWizEd , I modified my code as you said, but still not getting that variable. Any Idea..?
Sorry I had a slight typo. change template.TodaysUrl = TodaysUlr; to template.TodaysUrl = TodaysUrl; It works for me.
still not working and this time I got google drive error on ButtonB Pls check webApp and you have also access to App Script file , so pls modify code according to..
I've added another suggested edit.
I have modified according to your above suggested edit. but it still not working. pls check WebApp link
|

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.