动态加载JavaScript?

时间:2011-02-20 15:03:33

标签: extjs javascript

我在应用程序中使用了很多脚本,其中一些不需要加载应用程序,我想在可能之前加载它们,知道我的应用程序是用ExtJS编码的,并且使用了很多ajax调用

3 个答案:

答案 0 :(得分:4)

您可以查看使用LABjs这是一个脚本加载器。

  

老而破坏:

<script src="framework.js"></script>
<script src="plugin.framework.js"></script>
<script src="myplugin.framework.js"></script>
<script src="init.js"></script>
  

新热点:

<script>
   $LAB
   .script("framework.js").wait()
   .script("plugin.framework.js")
   .script("myplugin.framework.js").wait()
   .script("init.js").wait();
</script>

<强>更新: 如果你想加载jQuery,可以从blog post开始做这样的事情。

<script type="text/javascript" src="LAB.js">
if (typeof window.jQuery === "undefined") {
    $LAB.script("/local/jquery-1.4.min.js");
}

答案 1 :(得分:3)

您只能在需要时使用javascript加载外部文件:

function LoadJs(url){
  var js = document.createElement('script');

  js.type = "text/javascript";
  js.src = url;

  document.body.appendChild(js);

}

LoadJs("/path/to/your/file.js");

答案 2 :(得分:1)

这是一篇关于该主题的精彩文章,其中包含您可能想要查看的建议解决方案:http://www.nczonline.net/blog/2011/02/14/separating-javascript-download-and-execution/

相关问题