[英]jquery dynamic loading of external script files before $(document).ready()
Currently, I have a jqueryUI.js file referenced through a <script>
tag. 目前,我有一个通过
<script>
标记引用的jqueryUI.js文件。
I have several $(document).ready() functions, which use jquery ui functions. 我有几个$(document).ready()函数,它们使用jquery ui函数。
I am trying to load jqueryUI.js file dynamically using $.getScript 我试图使用$ .getScript动态加载jqueryUI.js文件
I tried the following code... 我尝试了以下代码......
var scriptCntr = 0;
$(document.ready(function(){
scriptCntr ++;
$.getScript(path, function (data, textStatus) {
scriptCntr --;
});
while(scriptCntr!=0){
}
});
on the top of the page. 在页面顶部。 The idea is to make the $(document).ready() wait till the file is downloaded, but the logic goes to the WaitLoop and stays there indefinitely.
我的想法是让$(document).ready()等到文件被下载,但逻辑进入WaitLoop并无限期地停留在那里。 The file wouldn't download unless all the $(document).ready() is executed.
除非执行所有$(document).ready(),否则不会下载该文件。
Is there a way to download the jqueryUI.js file before the first $(document).ready() is executed? 有没有办法在执行第一个$(document).ready()之前下载jqueryUI.js文件?
Thanks in advance. 提前致谢。
You can use jQuery.holdReady() . 您可以使用jQuery.holdReady() 。 This allows you to delay the jQuery's ready event until after the script has loaded.
这允许您延迟jQuery的ready事件,直到脚本加载完毕。
$.holdReady(true);
$.getScript("myplugin.js", function() {
$.holdReady(false);
});
I use $.getScript()
extensively for scripts that rely on jQuery
being downloaded first as $.getScript()
won't instantiate until jQuery is loaded (it's a method of jQuery itself, so can't run unless qQuery has loaded successfully, resolving a jQuery dependency). 我广泛使用
$.getScript()
来依赖首先下载jQuery
脚本,因为$.getScript()
在jQuery加载之前不会实例化(它是jQuery本身的一个方法,所以除非qQuery已成功加载,否则无法运行,解决jQuery依赖关系)。
A couple of side notes: 几个旁注:
$.getScript()
forces a download, it NEVER uses cached copies. $.getScript()
强制下载,它永远不会使用缓存副本。 cane be useful if you always want fresh downloads. 如果你总是想要新的下载,手杖是有用的。 You can use the
$.ajax()
method if you want to enable caching. 如果要启用缓存,可以使用
$.ajax()
方法。 This allows a SIGNIFICANT performance boost when downloading large files like jQuery-ui on subsequent pages. 这使得一个显着的性能提升下载大文件就像在随后的页面的jQuery UI的时候。 I will provide an example at the end of this answer.
我将在这个答案的最后提供一个例子。
Due to the $.getScript()
method using the jqxhr model, you can check for a failed script load, and the exception being thrown. 由于使用jqxhr模型的
$.getScript()
方法,您可以检查失败的脚本加载,并抛出异常。 The $.getScript()
function has a .fail()
handler attached, you can use it like this... $.getScript()
函数附加了一个.fail()
处理程序,您可以像这样使用它...
using $.getScript() 使用$ .getScript()
var urlToLoad = "testLoader.js";
$.getScript("js/slickGrid/jquery.event.drag-2.0.min.js",function(){
// Execute script on successful load
}).fail(function(jqxhr, settings, exception) {
// Code to execute if script fails to load any further
document.write('FAILED Script Load '+urlToLoad +': '+exception)
});
using $.ajax() (for caching purposes) 使用$ .ajax() (用于缓存目的)
var urlToLoad = "testLoader.js";
$.ajax({
dataType: "script",
url: "someurl.php",
cache: true,
success: function() {
// code to execute on script load success
}
}).fail(function(jqxhr, settings, exception) {
// Code to execute if script fails to load any further
document.write('FAILED Script Load '+urlToLoad +': '+exception)
});
I personally write error codes to the screen so I can see the stop point a little easier than in a console, and I have found that console.log output sometimes fails for me depending on my debug software. 我个人在屏幕上写错误代码,所以我可以看到停止点比在控制台中更容易,我发现console.log输出有时会失败,这取决于我的调试软件。 Helps a LOT for troubleshooting.
有助于排除故障。 Using this implementation, you can make a simple dependency loader.
使用此实现,您可以创建一个简单的依赖项加载器。 Define an array of scripts, execute a function to fire the
$.ajax()
function, and do something when they're all done. 定义一个脚本数组,执行一个函数来触发
$.ajax()
函数,并在它们全部完成时执行某些操作。 And voila, dependency resolved. 瞧,依赖性已经解决了。 ReuireJAS does this, but it's a set of scripts and additional lod times.
ReuireJAS这样做,但它是一组脚本和额外的lod时间。 You have jQuery already loaded, so why not do that!
你已经加载了jQuery,所以为什么不这样做呢!
Example of dependency resolution please keep credits if you use it... :) 依赖解析的例子, 如果你使用它,请保留信用... :)
/*-----------------------------------
* Script is free to use as long as
* credits stay attached to script here.
* jQuery Dependency Loader
* created: May 15, 2012
* author: Darren Maurer
* CEO/CIO/President
* Monarch Research International
* http://MonarchResearch.ca
using jQuery-1.7.2.min.js.php (caching enabled)
------------------------------------*/
// Array of fileName to include in loader
var urlsToLoadInOrder = ["js/file1.js","js/file2.js","js/file3.js"];
function ProcessDependency(urlArrayPointer) {
if (urlArrayPointer >= urlsToLoadInOrder.length) {
return;
}
var attemptedFile = urlsToLoadInOrder[urlArrayPointer];
$.ajax({
dataType: "script",
url: attemptedFile,
//Enable caching for performance, disable with 'cache: false'
cache: true,
success: function() {
// code to execute on script load success
urlArrayPointer++;
ProcessDependency(urlArrayPointer);
}
}).fail(function(jqxhr, settings, exception) {
// Code to execute if script fails to load any further
alert('FAILED Script Load '+attemptedFile +': '+exception);
return false;
});
}
// Set pointer to zero and instantiate the loader
ProcessDependency(0);
// All scripts successfully loaded in order, dependencies resolved
alert('loads complete');
Then just include these two lines on your main .html or .php page 然后在主.html或.php页面上包含这两行
<script type="text/javascript" src="js/jquery-1.7.2.min.js.php"></script>
<script src="ajaxDependencyLoader.js"> </script>
AND VOILA!!! 和VOILA !!! The easiest to use dependency loader I've found yet :)
我发现最容易使用的依赖加载器:)
The reason I use this method, is for dependency resolution between my scripts. 我使用此方法的原因是我的脚本之间的依赖性解决。 Each dependency set is defined as an array, and I loop through it, they load in order, and I'm happy :)
每个依赖集都被定义为一个数组,我遍历它,它们按顺序加载,我很高兴:)
You can use this code for this target: 您可以将此代码用于此目标:
var wf = document.createElement('script');
wf.src = 'http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.