简体   繁体   English

Head js加载javascript文件的问题

[英]Head js Problems with loading javascript files

I'm using Head js to load my javascript files in parallel. 我正在使用Head js并行加载我的JavaScript文件。 I added head js to my head and then used head.js("path/to/file/my.js"); 我将head js添加到我的头部,然后使用head.js("path/to/file/my.js"); but when I load my webpage the script is missing. 但是当我加载网页时,脚本丢失了。 Only after refreshing a few times does the whole script work properly. 只有刷新几次后,整个脚本才能正常工作。 Why do I need to refresh it to make it work? 为什么需要刷新才能使其正常工作? Any suggestions would be appreciated! 任何建议,将不胜感激!

As the scripts are loaded asynchronously, you can't use it immediately. 由于脚本是异步加载的,因此您不能立即使用它。 After you have refreshed the page, it will find the script in the cache, so it will load in time for any code needing it sometimes . 刷新页面后,它将在缓存中找到脚本,因此有时会及时加载任何需要它的代码。

Use the ready method for any code that needs the script: 对需要脚本的任何代码使用ready方法:

head.ready(function() {
  // any code that needs the script to be loaded first
});

Another way is to mark your library and then get the ready event when your script is loaded. 另一种方法是标记您的库,然后在加载脚本时获取ready事件。 Read more from http://headjs.com/ Labeling scripts. http://headjs.com/标签脚本中了解更多信息。

head.ready("your", function() {

});

head.js(
   {jquery: "http://http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"},
   {tools: "http://cnd.jquerytools.org/1.2.5/tiny/jquery.tools.min"},
   {your: "http://a.heavy.library/we/dont/want/to/wait/for.js"},

   // label is optional
   "http://can.be.mixed/with/unlabeled/files.js"
);

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

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