简体   繁体   English

在index.html上的js导入时定义变量

[英]Define a variable at an js import on index.html

I have a JavaScript script which a user can import on his website. 我有一个JavaScript脚本,用户可以在其网站上导入该脚本。 This script will basicly create an socket connection which transfers small messages which the script will display on the client side. 该脚本从根本上将创建一个套接字连接,该套接字连接将传输小消息,该小消息将显示在客户端。

I will have more than one user on more than one website. 我将在多个网站上拥有多个用户。 So I need an identifier to handle the identification of the website. 因此,我需要一个标识符来处理网站的标识。

I plan to use something like a CDN to host this script, so the user can just import it within his index.html . 我计划使用CDN类的东西来托管此脚本,因此用户可以将其导入到他的index.html The script does always the same logic but with different Connection Values. 该脚本始终执行相同的逻辑,但具有不同的连接值。 Im pretty sure I dont have to create the same files, with different identifier values, for every websites which will use my script, but so far i have no idea how to tell the script which identifier value it should use. 我很确定我不必为将要使用我的脚本的每个网站创建具有不同标识符值的相同文件,但是到目前为止,我还不知道如何告诉脚本应使用哪个标识符值。

Is there a good way to handle such problems? 有解决这种问题的好方法吗?

将连接ID​​存储在加载脚本的HTML文档中(例如<script src="http://cdn.example.com/your.js" data-connection-id="74893872"></script> ),拉使用您的JS从DOM中获取。

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

相关问题 如何将src文件中的js文件导入index.html? - How to import js file from src file into index.html? ExpressJS:将.js文件中的变量调用为index.html - ExpressJS: call a variable from .js file to index.html 在index.html文件中使用JS变量 - Use JS variable inside index.html file 如何从js获取变量的值到index.html - How to get variable's value from js to index.html 如何在 index.html 文件中定义变量? (window.parentPage = true;) - How do I define a variable in my index.html file? (window.parentPage = true;) 如何将Vuejs组件“导入”到index.html? - How to 'import' Vuejs component into index.html? 如何更正导入 JS 文件到另一个 JS 文件然后到 index.html? - How to correct import JS file to another JS file and then to index.html? 使用 fetch + node.js 将 HTML + JavaScript 变量从外部 js 打印到 index.html 文件 - Printing a HTML + JavaScript variable from an external js to an index.html file using fetch + node.js 如何将 index.html/index.jsp 上定义的 Javascript 变量用于 Vue.js? - how to make use of Javascript variable which are defined on index.html/index.jsp to Vue.js? 从Node.js + Ionic应用程序的index.html中的其他文件夹导入js文件时出现404错误 - 404 error while import js file from another folders in index.html in nodejs + Ionic app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM