Permalink
Browse files

fix script-loader.js

  • Loading branch information...
1 parent 9a3d368 commit 4b4a3396ce6ba84191a725503aefd0e8a7efd575 @liunian liunian committed Jun 18, 2012
Showing with 16 additions and 12 deletions.
  1. +16 −12 lib/script-loader.js
View
@@ -1,17 +1,21 @@
function loadScript(path, callback) {
- var script = document.createElement("script");
- if (script.onload == null) {
- script.onload = function () {
- callback();
- }
- } else if (script.onreadystatechange == null){
- script.onreadystatechange = function () {
- if (this.readyState == "load") {
+ var script = document.createElement('script');
+ // ie9 同时支持 onreadystatechange 和 onload
+ if (script.onreadystatechange) {
+ script.onreadystatechange = function() {
+ var st = script.readyState;
+ if (st === 'loaded' || st === 'complete') {
callback();
+ // 清空,以免执行 2 次
+ script.onreadystatechange = null;
}
- }
+ };
+ } else {
+ script.onload = function() {
+ callback();
+ };
}
-
+
script.src = path;
- document.getElementsByTagName("head")[0].appendChild(script);
-}
+ document.getElementsByTagName('head')[0].appendChild(script);
+}

0 comments on commit 4b4a339

Please sign in to comment.