Anonymous Anonymous
Anonymous Anonymous

Reputation: 1

How can I pass a variable from an HTML script file to a javascript function in google apps script?

I would like to get the users ip, and then load it onto the properties service. But, the html file script uses javascript, not google apps script, so

<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>

<script type="text/javascript">
   PropertiesService.getScriptProperties().setProperty(String(userip), "1")
</script> 

doesn't work. How can I make it work? Would be appreciated. Edit: context below

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" />
    <base target="_top">

  </head>
  <body>
    `_insert webpage here_`
    <script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>
    <script type="text/javascript">
      PropertiesService.getScriptProperties().setProperty(String(userip), "1")
    </script>
  </body>
</html>

Upvotes: 0

Views: 173

Answers (1)

LJHarb
LJHarb

Reputation: 39314

Depending on your backend language, you can insert the value dynamically - if, say, it's PHP, you could do <%= $userIP %> in the middle of that inline JS code.

However, best practices would dictate that a) your JS code all lives in a .js file, and b) there's only three ways to properly get data from the server to the client:

  1. AJAX request from JS.
  2. HTML data-* attributes on some arbitrary element(s), read them in JS.
  3. <script type="application/json"></script>, read and parse it from JS, however this option requires special escaping of in the json body

Upvotes: 1

Related Questions