对于JavaScript来说,我们要引入外部脚本的方式如下:
<script type="text/javascript" scr="xxx.js"></script>
通常,大多数浏览器都支持并行下载组件,但对于外部脚本并非如此。当浏览器开始下载外部脚本时,在脚本下载,解析并且执行完毕之前,不会开始下载任何其他内容。也就是任何其他内容都会被阻塞掉。
所以如何使得脚本不阻塞,是前端开发人员所必须注意的一个问题。有以下技术在某些浏览器中可以实现并行下载:
//This file should be used if you want to debug and develop
function jqGridInclude()
{
var pathtojsfiles = "js/"; // need to be ajusted
// set include to false if you do not want some modules to be included
var modules = [
{ include: true, incfile:'i18n/grid.locale-en.js'}, // jqGrid translation
];
var filename;
for(var i=0;i<modules.length; i++)
{
if(modules[i].include === true) {
filename = pathtojsfiles+modules[i].incfile;
if(jQuery.browser.safari) {
jQuery.ajax({url:filename,dataType:'script', async:false, cache: true});
} else {
if (jQuery.browser.msie) {
document.write('<script charset="utf-8" type="text/javascript" src="'+filename+'"></script>');
} else {
IncludeJavaScript(filename);
}
}
}
}
function IncludeJavaScript(jsFile)
{
var oHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.setAttribute('type', 'text/javascript');
oScript.setAttribute('language', 'javascript');
oScript.setAttribute('src', jsFile);
oHead.appendChild(oScript);
}
}
jqGridInclude();