2011-02-08 53 views
7

不會呈現我創建它使用的Cufón,並在頁面重量方面特別重的部位,由於大量的Javascript。所以我想在腳本異步地head.js(http://headjs.com/)像這樣加載:的Cufón異步加載在IE

head.js("http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js", function() { 
head.js("/js/libs/cufon-yui.js", function() { 
    head.js("/js/shared/Stag_Bold_700.font.js" , function() { 
      Cufon.replace('h1', { fontFamily: 'Stag Bold' }); 
    }); 
}); 
}); 

所以jQuery是第一次下載,後續的CufónLIB文件和字體的Cufón按順序下載,然後是的Cufón最後呼籲取代H1。顯然,這是一個減少替換的例子,但是當試圖替換H1時仍然不起作用。

的問題是,只有在Internet Explorer(6/7/8),文本不會被替換,但我可以看到的Cufón肯定已經被調用。我可以確定這一點,因爲標籤中添加了「cufon-active cufon-ready」類。當我使用IE Developer工具欄檢查標記時,cufon/cufoncanvas標籤存在於選定的元素中,但由於缺乏更好的單詞,它們不可見。

在IE9中,按照預期類似於Chrome和Firefox的行爲腳本。我已經嘗試調整Cufon繪圖引擎,並已更新到最新的1.09i版本,以獲得更好的效果。如果我在打電話的Cufón報表移到文檔準備事件而不是裝載的異步方式,它的工作原理,但我想優化頁面加載和我的網站將使用數量的Cufón字體,以及許多其他的JS插件。我也嘗試使用labs.js和head.js異步加載適當的文件。

+4

也很爛,你應該提交一個錯誤。 – Marko 2011-02-08 09:14:23

+0

Fix爲[合併](https://github.com/sorccu/cufon/commit/79ea413a3aadc30d8cffb05faade6e003d9e7e5a)到主存儲庫。 – 2012-02-10 22:09:02

回答

2

我有同樣的問題 - 我解決了這個利用head.js的瀏覽器檢測到做到以下幾點:

if (head.browser.mozilla || head.browser.webkit || head.browser.opera || 
     (head.browser.ie && (head.browser.version == '9.0'))) { 
     head.js('script/jquery.min.js', 
       'script/cufon.js', function() { 
        head.js('script/bebas_neue_400.font.js', function() { 
         Cufon.replace('h1', { 
          textShadow: '1px 1px rgba(0, 0, 0, 0.2)' 
         }).now(); 
         // or a head.js('scripts/file.with.cufon.replacement.js'); 
        }); 
       }); 
      } else { 
     // here we load scripts depending on GZIP support for this browser 
     document.write('\x3Cscript type="text/javascript" src="script/jquery.min.js">\x3C/script>'); 
     document.write('\x3Cscript type="text/javascript" src="script/cufon.js">\x3C/script>'); 
     document.write('\x3Cscript type="text/javascript" src="script/bebas_neue_400.font.js">\x3C/script>'); 
     document.write('\x3Cscript type="text/javascript" src="script/file.with.cufon.replacement.js">\x3C/script>');    
    } 

你也可以使用條件的意見(我沒有,因爲我也做在JavaScript GZIP支持檢測和調整這些動態加載腳本需要。)

這是一個黑客,但直到它的庫本身內處理應該是足夠用。

(我還貼出GIST一個更完整的例子here

+0

我很傷心。因爲它隻影響cufon,你仍然可以在ie部分使用head.js作爲jquery等,但是單獨加載cufon還是? – worenga 2011-04-21 12:48:12

2

嘗試調用

<script type="text/javascript"> Cufon.now(); </script> 

</body>標籤即將關閉。

+1

這可能會在Cufon有機會被加載腳本下載之前調用:SCRIPT5009:'Cufon'未定義。我也嘗試在Cufon.replace調用後立即放置此行,但仍然沒有任何結果 – giles 2011-02-08 09:33:56

+0

這沒有奏效。 – giles 2011-02-13 22:28:08

1

嘗試Cufon.replace電話後加入Cufon.now(),就像這樣:

Cufon.replace('h1', { fontFamily: 'Stag Bold' }); 
Cufon.now(); 
0

我類似CameraSchoolDropout的做法的方式解決了這個,只是不使用Document.write,我使用的是IE條件的標籤,並YepNope.js

This issue on github說他們有問題使用document.createElement('script'),我只是感覺使用IE條件更好。

你可以看到,我在http://epraxadev.com/yepnope/

<head> 

<style type="text/css"> 
#txt { visibility:hidden; } 
</style> 

<!--[if lte IE 8]> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script src="js/cufon-yui.js"></script> 
<script src="js/adventor.cufon.js"></script> 
<![endif]--> 

<script src="js/modernizr.custom.js"></script> 
<script> 
yepnope([{ 
    test: window.jQuery, 
    nope: { 
     'yJ': '//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', 
     'yCufon': 'js/cufon-yui.js', 
     'yFont': 'js/museo.font.js' 
    }, 
    callback: { 
     'yJ': function(){ 
      console.log("YepNope loaded jQuery! This isn't IE!"); 
     } 
    }, 
    complete: function() { 
     console.log('All browsers, including IE, will show this'); 

     Cufon.replace('h1'); 

     $(document).ready(function(){ 
      $('#txt').css('visibility', 'visible'); 
     }); 
    } 
}]); 
</script> 

<noscript> 
    <style type="text/css"> 
    #txt { visibility:visible; } 
    </style> 
</noscript> 

</head> 
0

創建現在只使用普通<script>標籤加載jQuery和的Cufón一個例子頁面,將使用腳本加載後續文件。

使用document.write是一種不好的方法,因爲它只有在DOMReady之前加載/執行腳本並使用瀏覽器嗅探來執行時纔有效,因爲它可能會給出錯誤結果。

有條件的意見是不是一個好的解決方案,因爲你可能需要在將來更新腳本,你必須記住在2米不同的地方是不好的維護更新它。

按照this issue on GitHub知道什麼時候該錯誤是固定的。