[英]How to import src for a script in index.html file
Currently in my index.html
file I have the following script line: 当前,在我的
index.html
文件中,我具有以下脚本行:
<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places"></script>
Is it possible to extract the src
variable and import it in the script as a variable? 是否可以提取
src
变量并将其作为变量导入脚本中? I tried using xsl:variable
located in an external variable.xsl
file such as this: 我尝试使用位于以下外部
variable.xsl
文件中的xsl:variable
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="google" select="API_KEY"></xsl:variable>
</xsl:stylesheet>
And then trying to import it into my script, 然后尝试将其导入到我的脚本中,
<xsl:include href="variable.xsl"/>
<script src="{$google}"></script>
But even when the xls:variable
is in the same file, the script source doesn't work. 但是,即使
xls:variable
在同一个文件中,脚本源也不起作用。
I would like to push my code to a public repo, which is why I'd like to hide the key. 我想将我的代码发布到公共仓库中,这就是为什么我想隐藏密钥。
Hope this is the answer you are looking for, by simply using JQuery you could add the src
attribute dynamically 希望这是您要寻找的答案,只需使用JQuery即可动态添加
src
属性
HTML 的HTML
<script id="googleAPI">
Notice we set an id on the script element 注意,我们在脚本元素上设置了一个id
Javascript Java脚本
let url = "https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places"
$("#googleAPI").attr("src", url)
You cannot hide something client side, only obfustucate it (make it harder to see). 您不能隐藏客户端的某些内容,而只能对其进行混淆(使其更难看到)。 If you use the xsl file, I can make a request and see that, if you set the dynamically create the script, I can open the network tab see where the request is going.
如果您使用xsl文件,则可以发出请求,并看到,如果您设置了动态创建脚本,则可以打开“网络”选项卡,查看请求的去向。 If you want to hide the key:
如果要隐藏密钥:
"https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places"
"https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places"
发出请求 text/plain
or convert it to dataURl
and then send it to client side where you create a script
tag and set the textContent
, innerHTML
, or create an object url from Blob
(having the server response as Blob
part) and set src etc. text/plain
获取内容或将其转换为dataURl
,然后将其发送到客户端,在此处创建script
标签并设置textContent
, innerHTML
或从Blob
创建对象url(将服务器响应作为Blob
部分)并进行设置src等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.