[英]How to pass an element-value variable from Javascript html file to code.gs file in google apps script?
[英]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)只有三種方法可以正確地從服務器獲取數據到客戶端:
data-*
屬性,可在JS中讀取。 <script type="application/json"></script>
,從JS讀取並解析它,但是此選項需要在json主體中進行特殊轉義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.