簡體   English   中英

如何將變量從HTML腳本文件傳遞到Google Apps腳本中的javascript函數?

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

我想獲取用戶ip,然后將其加載到屬性服務上。 但是,html文件腳本使用javascript而不是google apps腳本,因此

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

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

不起作用。 我該如何運作? 將不勝感激。 編輯:下面的上下文

<!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>

根據您的后端語言,您可以動態地插入值-例如,如果是PHP,則可以在該內聯JS代碼中間執行<%= $userIP %>

但是,最佳實踐將要求a)您的JS代碼全部都保存在.js文件中,並且b)只有三種方法可以正確地從服務器獲取數據到客戶端:

  1. 來自JS的AJAX請求。
  2. 一些任意元素上的HTML data-*屬性,可在JS中讀取。
  3. <script type="application/json"></script> ,從JS讀取並解析它,但是此選項需要在json主體中進行特殊轉義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM