简体   繁体   English

从javascript代码在DOM中添加外部脚本

[英]Adding external scripts in DOM from javascript code

I have included 3 external js files at the end of body. 我在正文末尾包含了3个外部js文件。 Suppose my document already contains a js named as insertlibs.js and here is the code 假设我的文档已经包含一个名为insertlibs.js的js,这是代码

var script1 = document.createElement('script');
script1.src='http://code.jquery.com/jquery-latest.js';
script1.type='text/javascript';
document.getElementsByTagName('Body').item(0).appendChild(script1);

// Similar way to include underscore

var script2 = document.createElement('script');
script2.src='hhttp://documentcloud.github.com/backbone/backbone-min.js';
script2.type='text/javascript';
document.getElementsByTagName('Body').item(0).appendChild(script2);  

But what is happening sometimes, it is throwing an error that $ is not defined and I tried to debug in Firefox and there is a parallel download occurring for jquery and backbone and sometimes backbone library getting download earlier than jQuery which is causing this error. 但是有时发生的事情是,它抛出一个错误,即未定义$,我尝试在Firefox中进行调试,并且jquery和骨架的并行下载发生了,有时骨架库的下载早于jQuery,这导致了此错误。

As far as i know that if a script tag is included, it will block further request So as soon as I add jquery in dom. 据我所知,如果包括一个脚本标记,它将阻止进一步的请求。所以,一旦我在dom中添加jquery。 I am confused about the workflow here happening. 我对这里正在发生的工作流程感到困惑。

So i have found the solution, I merged both the js and making a single call which is working perfectly but that does not explain me the flow happening in above case. 因此,我找到了解决方案,我合并了两个js,并进行了一次正常调用,但这并不能解释上述情况下发生的流程。 Please help. 请帮忙。

This is because you are attempting to include backbone without ensuring that jquery has been completely loaded. 这是因为您尝试包括骨干网而未确保jquery已完全加载。 To correct this, you can use the script's onload attribute to attach a callback which will be fired when jquery is loaded. 为了解决这个问题,您可以使用脚本的onload属性附加一个回调,该回调将在加载jquery时触发。

For ex: 例如:

var script1 = document.createElement('script');
script1.src='http://code.jquery.com/jquery-latest.js';
script1.type='text/javascript';

// add an onload handler
script1.onload = function() {

   // load the rest of the scripts here

   var script2 = document.createElement('script');
   script2.src='hhttp://documentcloud.github.com/backbone/backbone-min.js';
   script2.type='text/javascript';
   document.getElementsByTagName('Body').item(0).appendChild(script2);
}

document.getElementsByTagName('Body').item(0).appendChild(script1);

As far as i know that if a script tag is included, it will block further request 据我所知,如果包含脚本标签,它将阻止进一步的请求

No, the blocking / synchronous download is only when the tags are right in the parsed HTML (or are inserted via document.write during the parse); 不,仅当标签正确位于已解析的HTML中(或在解析期间通过document.write插入)时,才进行阻止/同步下载。 dynamically DOM-appended scripts load asynchronously and in parallel. 动态添加了DOM的脚本以异步和并行方式加载。

To do that but ensure that scripts are executed when their dependencies are met, you need to use AMD loaders. 为此,但要确保在满足脚本依赖项时执行脚本,您需要使用AMD加载程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM