JS動態載入CSS和JS

2022-02-09 00:03:55 字數 529 閱讀 9212

這兩天工作時用到動態載入css和js的地方比較多,這裡稍微做下整理。

if (!this.readystate || this.readystate === 'loaded' || this.readystate === 'complete')

因為在ie中使用onreadystatechange, 而gecko,webkit 瀏覽器和opera都支援onload。事實上this.readystate == 'complete'並不能工作的很好,理論上狀態的變化是如下步驟: 

0 uninitialized 

1 loading 

2 loaded 

3 interactive 

4 complete 

但是有些狀態會被跳過。根據經驗在ie7中,只能獲得loaded和completed中的乙個,不能都出現,原因也許是對判斷是不是從cache中讀取影響了狀態的變化,也可能是其他原因。最好把判斷條件改成this.readystate == 'loaded' || this.readystate == 'complete' 

動態載入js

方法1 getscript themes sh public xtpla js jquery.plugins.min.js 方法2 載入多個js檔案 如圖所示 一 target.insertbefore newchild,existingchild 引數說明 1.target 被新增節點和現有節點的...

動態載入js檔案

var obj new object var url chrome content js components.classes mozilla.org moz jssubscript loader 1 getservice components.inte ces.mozijssubscriptloa...

動態載入js檔案

最近在維護乙個專案,看到乙個頁面竟然要載入20多個js檔案!真驚人,仔細觀察後,發現有很多js檔案根本用不上的,或者在某個條件成立才使用的 為了提高此頁面的效能,決定使用動態載入js檔案的方法,即在需要的時候才載入該js檔案。網上看了很多關於動態載入js的方法,例子挺多的,但是都有些不足的地方。動態...