一个 .js 加载所有 .js 和 .css
我需要创建一个简单的 javascript 嵌入类型,用户在其网站中添加少量 javascript,然后我的脚本完成其余的工作。
这是我希望在用户站点上看到的内容:
包含一次:
<script type="text/javascript" src="path/to/doEverythingScript.js"></script>
在 HTML 中:
<div id="target-div">
<script type="text/javascript">
doThis(1714581, "#target-div");
</script>
</div>
我还需要能够在同一页面上运行多个版本。
id(例如:1714581)与我的目标 div 一起传递到我的函数。
这个问题是我需要让一个脚本加载所有依赖项:
例如:
path/to/style1.css
path/to/style2.css
path/to/style3.css
path/to/jquery.js
path/to/script1.js
path/to/script2.js
path/to/script3.js
path/to/script4.js
一旦它们全部加载,我就可以运行我的函数。
如果我的函数在它们全部加载之前运行它自然就无法工作。
我尝试使用 LazyLoad 和 LAB,但无法弄清楚如何让一个 JS 文件仅使用一个链接脚本和页面上的一个小函数来对所有这些进行排序。
到目前为止我所写的内容不起作用,因为该函数尝试在加载依赖项之前运行。
非常感谢对此问题的任何帮助。
I need to create a simple javascript embed type thing where the user adds a small amount of javascript to their site and my script does the rest.
This is the sort of thing I would like to see on the user site:
Included once:
<script type="text/javascript" src="path/to/doEverythingScript.js"></script>
And this in the HTML:
<div id="target-div">
<script type="text/javascript">
doThis(1714581, "#target-div");
</script>
</div>
I would also need to be able to have multiple versions running on the same page.
The id (eg: 1714581) is passed to my function along with my target div.
This issue is I need to have the one script load all the dependancies:
eg:
path/to/style1.css
path/to/style2.css
path/to/style3.css
path/to/jquery.js
path/to/script1.js
path/to/script2.js
path/to/script3.js
path/to/script4.js
And once they are all loaded I would then be able to run my function.
If my function ran before they all loaded It naturally wouldn't work.
I tried using LazyLoad and LAB but couldn't work out how to have one JS file sort all this out with only one linkied script and a small function on the page.
What I have writen so far doesnt work becuas the function tries to run before the dependecies are loaded.
Any help with this issue is much appreciated.
发布评论
评论(2)
让它成为您加载的脚本(配置第一个块以满足您的愿望)。根据OP的要求,我还添加了一个队列功能。
注意:如果您想延迟加载其他脚本直到之前的请求完成,请添加
_REQUIRED_
而不是外部资源。在您的 HTML 源代码中:
Let this be your loaded script (configure the first block to meet your wishes). On request of the OP, I've also added a queue function.
Note: Add
_REQUIRED_
instead of an external resource, if you want to delay loading additional scripts until the previously requests has been finished.In your HTML source:
您是否尝试过使用 document.write() 在页面中插入新的
和