簡體   English   中英

Head js加載javascript文件的問題

[英]Head js Problems with loading javascript files

我正在使用Head js並行加載我的JavaScript文件。 我將head js添加到我的頭部,然后使用head.js("path/to/file/my.js"); 但是當我加載網頁時,腳本丟失了。 只有刷新幾次后,整個腳本才能正常工作。 為什么需要刷新才能使其正常工作? 任何建議,將不勝感激!

由於腳本是異步加載的,因此您不能立即使用它。 刷新頁面后,它將在緩存中找到腳本,因此有時會及時加載任何需要它的代碼。

對需要腳本的任何代碼使用ready方法:

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

另一種方法是標記您的庫,然后在加載腳本時獲取ready事件。 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