简体   繁体   English

使用head.js以正确的顺序正确加载脚本和依赖项?

[英]Properly loading scripts and dependencies in correct order using head.js?

I am not sure if I am utilizing head.js correctly. 我不确定我是否在正确使用head.js。 In the header of my html document I call the head.js file via: 在我的html文档的标题中,我通过以下方式调用head.js文件:

<script src="/scripts/head.min.js" type="text/javascript"></script>

Then right before the closing < / body > tag in the html page, I call the following file: 然后在html页面中的</ body>标记之前,我调用以下文件:

<script src="/scripts/load.js" type="text/javascript"></script>

In the load.js file I have the following code: 在load.js文件中,我有以下代码:

head.js(

   {livechat: "/scripts/livechat.js"},
   {jquery: "http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"},
   {jquerytools: "http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"},
   {slider: "/scripts/jquery.nivo.slider.pack.js"},
   {prettyphoto: "/scripts/jquery.prettyPhoto.js"},
   {sliderfunctions: "/scripts/slidercode.js"},
   {functions: "/scripts/functions.js"}

);

Does the above code cause the javascript files to execute in the same exact order they are listed or do they sometimes execute out of order? 上面的代码是否导致javascript文件按照列出的确切顺序执行,或者有时会不按顺序执行?

I ask because the slider initially only functioned if I utilized the following code within load.js: 我问是因为滑块最初仅在我使用load.js中的以下代码时才起作用:

head.ready("slider", function() {
   $('#slider').nivoSlider({
      effect:'sliceDown',
      controlNav: false
   });
});

I was able to get around this by moving the above code to an external file called slidercode.js which contained the following code: 我可以通过将上面的代码移动到名为slidercode.js的外部文件来解决此问题,该文件包含以下代码:

$(window).load(function() {
$('#slider').nivoSlider({
        effect:'sliceDown',
        controlNav: false
    });
});

But I am not sure if I am going about this the correct and most efficient way as this is my first time using head.js. 但是我不确定是否要以正确,最有效的方式进行操作,因为这是我第一次使用head.js。 Basically from the javascript files in loader.js I need to make sure: 基本上从loader.js中的javascript文件中,我需要确保:

  1. jquery loads first. jQuery首先加载。
  2. Once jquery has fully loaded then jquerytools loads jQuery完全加载后,jquerytools将加载
  3. After jquery is fully loaded, it should load slider first and then prettyphoto. 完全加载jquery之后,应该先加载滑块,然后再加载prettyphoto。
  4. Then sliderfunctions should load as it is dependent on slider, 然后应该加载滑块功能,因为它取决于滑块,
  5. Lastly, functions should load as it is dependent on jquery and jquerytools. 最后,函数应该加载,因为它依赖于jquery和jquerytools。

You should call the scripts like this: (remove the http as well, it's not needed. Also Jquery always loads first of any scripts. in this case headJS has priority then jquery then all your scripts) 您应该这样调用脚本:(也删除http,也不需要。同样,jQuery总是首先加载所有脚本。在这种情况下,headJS优先,然后是jquery,然后是所有脚本)

<script src="/scripts/head.min.js" type="text/javascript"></script>
// this loads asyncrounously & in parallel
head.load("//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js", "//cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js", "/scripts/jquery.nivo.slider.pack.js", "/scripts/jquery.prettyPhoto.js", "/scripts/slidercode.js", "/scripts/functions.js");

Then right before the closing < / body > tag in the html page, I call the following file: 然后在html页面中的</ body>标记之前,我调用以下文件:

<script>
head.ready(function () {
        // some callback stuff
        $('#slider').nivoSlider({
    effect:'sliceDown',
    controlNav: false
});
    });
<script>

Does the above code cause the javascript files to execute in the same exact order they are listed or do they sometimes execute out of order? 上面的代码是否导致javascript文件按照列出的确切顺序执行,或者有时会不按顺序执行?

Yea, they load asyn, but execute in order. 是的,它们加载了asyn,但是按顺序执行。

I think head.ready("slider", function() {} ); 我认为head.ready("slider", function() {} ); should also work even if you place it outside of load.js . 即使将其放置在load.js之外,它也应该load.js Try adding it after load.js script block. 尝试在load.js脚本块之后添加它。

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

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