开发者

headjs order of parsing

I'm looking into headjs (http://www.headjs.com) to speed up loading of javascripts. From the documentation it's obviously possibly to control the execution order of scripts, however, parsing order is a different matter and is, afaik, undetermined.

Before going this route I'd like to know if the following would potentially give problems:

load jquery.js and scripts.js in parallel, like:

head.js("//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js","/js/script.js");

scripts.js has, among other things, some functions that extend jquery, like:

$.fn.extend({ myMethod: function(){...} });

Would this potentially give a parsing-problem ('$' or 'jquery' undefined) when scripts.js happens to be loaded (and parsed) before jquery.js or is the definition of '$' only checked on execution of the scripts? (which wouldn't not give problems, since scripts.js is executed after jquery)

I tested things out a bit and regardsless of the order: head.js("//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js","/js/script.js");

and head.js("/js/script.js"开发者_JAVA技巧,"//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js");

the extension seems to work when doing:

head.ready(function() { $('#someel').myMethod(); });

Somehow, I feel glad it works, but at the same time I'm careful to believe it would work each time. After all, when defining 'scripts.js' to be executed before 'jquery.js' I expect '$' to be undefined.

Some light on the matter would be appreciated!

Thanks, Geert-Jan


By the head.js docs the ready method will fire:

after all scripts have been loaded and the document is scriptable

So your code in head.ready should be OK regardless of the inclusion order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜