动态脚本标记加载不能按预期工作 [英] dynamic script tag loading is not working as expected

查看:106
本文介绍了动态脚本标记加载不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序同时使用谷歌闭包和dojo库。我们在我们的索引页面中具有以下功能:

We have an application that uses both the google closure and dojo libraries. We have the following in our index page which works as expected:


<script type="text/javascript" src="runtime/src/lib/google-closure-rev26/closure/goog/base.js"></script>
<script type="text/javascript" src="runtime/src/lib/dojo_release_132_src/dojo/dojo.js"></script>
<script type="text/javascript" src="runtime/src/core/loader.js"></script>

我们只想在实际的html源中使用一个脚本标签。所以我们尝试执行以下操作:

We would like to use only one script tag in the actual html source. So we tried to do the following:


<head>
   <script type="text/javascript" src="runtime/src-bootstrap.js"></script>
</head>

然后在src-bootstrap.js中:

and then in src-bootstrap.js:


var head = document.getElementsByTagName("head")[0];

var s1 = document.createElement("script");
s1.type = "text/javascript";
s1.src = "runtime/src/lib/google-closure-rev26/closure/goog/base.js";


var s2 = document.createElement("script");
s2.type = "text/javascript";
s2.src = "runtime/src/lib/dojo_release_132_src/dojo/dojo.js";

var s3 = document.createElement("script");
s3.type = "text/javascript";
s3.src = "runtime/src/core/loader.js";

head.appendChild(s1);
head.appendChild(s2);
head.appendChild(s3);

但是,这在FF中不起作用。 core / loader.js在dojo完全加载之前运行。任何想法为什么这不工作?

However, this doesn't work in FF. core/loader.js runs before dojo is loaded completely. Any ideas why this doesn't work?

推荐答案

对于这种类型的机制,你最好使用document.write ()来包括你的脚本。您目前使用的技术适用于延迟加载脚本,它会异步下载和执行脚本: http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/

For this type of mechanism, you'd be better off using document.write() to include your scripts. The technique you're currently using is suited to lazy-loading scripts, and it downloads and executes the scripts asynchronously: http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/

...或者你可以有一个实际连接这些文件的构建过程,只需请求一个脚本,这将节省请求的数量,因为你实际做的是增加请求的数量。

...or you could have a build process that actually concatenates these files, and just request the one script, which would save on the number of requests too, as what you've actually done is increased the number of requests.

这篇关于动态脚本标记加载不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆