Non-blocking JavaScript IE 對 window.onload

Nicholas C. Zakas 的 The truth about non-blocking JavaScript 有提到:

These three techniques for non-blocking JavaScript ensure that the downloading of the resource doesn’t block either rendering or the download of other resources on the page during page load. The little-known or understood aspect of all three approaches is that they are all defined to block the load event. That means adding scripts using any of these techniques during page load will delay execution of the window.onload event handler until all scripts have executed. All browsers obey this behavior except for Internet Explorer (even through version 9).
中文的意思是, Non-blocking 的所有方法都會對 onload 事件造成延遲、必須要等到指定的 JavaScript 讀取完畢才會真正執行,但 IE 是唯一的例外 。所以 在 IE 當網頁實際讀取完畢(onload)的時間點早於 JavaScript 被執行的時間點 時,JavaScript 中所撰寫的 window load 事件就不會生效了...

範例網頁 :除了 IE,其他瀏覽器都會跳出 alert 訊息。

所衍生的問題

看起來,似乎我們只要避免去綁 window 的 load 事件即可,但是 現在的 JavaScript 函式庫大多有做一些偵測 ,例如我們想要當某個 div 內容讀取完畢時就做某些動作,此時便使用 YUI 的 contentready 事件,但是 contentready 事件又會去檢查 window 的 load 事件,如果剛好又滿足前段所提的條件時,我們 這個 contentready 就永遠不會被觸發

目前只能先改用不會檢查 load 的 available 方法來達成,但如果大家有好方法不妨也提出來。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值