`function loadScript() { var script = document.createElement('script'), head = document.getElementsByTagName('head')\[0\]; script.type = 'text/javascript'; script.charset = 'UTF-8'; script.src = urls\[num\]; if (script.addEventListener) { script.addEventListener('load', function () { num = num+1; if(num < urls.length) { loadScript(); }else if(num == urls.length){ jQuery(document).ready(function($) { $('#multiselect').multiselect(); }); } }, false); } else if (script.attachEvent) { script.attachEvent('onreadystatechange', function () { var target = window.event.srcElement; if (target.readyState == 'loaded') { num = num+1; if(num < urls.length) { loadScript(); } }else if(num == urls.length){ jQuery(document).ready(function($) { $('#multiselect').multiselect(); }); } }); } head.appendChild(script); } function loadJsWithCommon(){ loadScript(); } function loadCommon(jsPath){ urls.push(jsPath); loadScript(); waitLoad(); waitLoadsetInterval(); sessionStorage.setItem("loaded",""); console.log("最後執行"); document.addEventListener("DOMContentLoaded", function(event) { console.log("DOM fully loaded and parsed"); console.log("1是否有加載了loaded判斷"+sessionStorage.getItem("loaded")); }); }javascript
var waitLoad = function(){ if(num < urls.length){ setTimeout("waitLoad()", 300) } }java
function waitLoadsetInterval(){ var timer= window.setInterval(function () { console.log("waitLoadsetInterval 判斷"); if(num >= urls.length){ console.log("js加載完成"); window.clearInterval(timer); angular.element(document).ready(function() { console.log("2是否有加載了loaded判斷"+sessionStorage.getItem("loaded")); if(!sessionStorage.getItem("loaded")){ console.log("獲取的ng-app名稱"+document.getElementsByTagName("body")\[0\].attributes\["ng-app"\].value); angular.bootstrap(document.getElementsByTagName("body")\[0\], \[document.getElementsByTagName("body")\[0\].attributes\["ng-app"\].value\]); } }); } }, 1000); }`bootstrap
問題是<body ng-app="" 在js異步加載沒完成就啓動了 因此找不到模塊 因此咱們監聽js加載完再去判斷一次 有執行過咱們就不用執行 沒腳本在啓動一次 ------相似天龍八部裏面的珍瓏棋局邏輯……session