简体   繁体   English

如何在index.html文件中为脚本导入src

[英]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: 如果要隐藏密钥:

  • make a request from your own server to "https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places" 从您自己的服务器向"https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places"发出请求
  • get the content as 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标签并设置textContentinnerHTML或从Blob创建对象url(将服务器响应作为Blob部分)并进行设置src等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将src文件中的js文件导入index.html? - How to import js file from src file into index.html? 如何将文件 vuejs 组件导入 index.html? - how can I import a file vuejs component into index.html? Webpack Babel 脚本添加到我的索引中。html 文件需要是 src="/main.js" 但是 src="main.js"。 如何正确配置它? - Webpack Babel script added to my index.html file needs to be src="/main.js" but is src="main.js". How can I configure this correctly? 如何将Vuejs组件“导入”到index.html? - How to 'import' Vuejs component into index.html? 如何使用angular将时间戳添加到index.html中的src标签 - How to add timestamp to tag src in index.html using angular Angular 在 index.html 中导入脚本并在组件中使用 - Angular import script in index.html and use it in component 如何使 Vue 从 assets 文件夹中导入文件,而不是 index.html 内的硬编码脚本标签 - How to make Vue import files from assets folder instead of hardcoded script tags inside of index.html 如何更正导入 JS 文件到另一个 JS 文件然后到 index.html? - How to correct import JS file to another JS file and then to index.html? script.js文件没有链接到index.html - script.js file not linking to index.html 如何知道何时导入包或将其粘贴到 index.html - How to know when to import a package or paste it in index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM