我正在查找允許我使用JavaScript以非異步方式加載其他JavaScript文件的代碼。我試過這個:JavaScript - 如何以非異步方式加載外部腳本?
var script=document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src", "jquery.min.js");
document.getElementsByTagName("head")[0].appendChild(script);
$("div.whatever").css("color","red");
但這並不奏效。它會插入腳本節點,但它會在jQuery加載之前繼續運行腳本的其餘部分。這最終失敗,因爲jQuery代碼嘗試運行時尚未由jQuery定義$
。
有沒有辦法來加載使用本地JS該腳本非異步?
可能的重複:http://stackoverflow.com/questions/3248384/document-createelementscript-synchronously – 2012-02-09 16:41:56
可能的重複[動態加載JavaScript同步](http://stackoverflow.com/questions/2879509/dynamically-loading- javascript-同步) – Bakudan 2012-02-09 16:42:49
^- 可能重複的評論^^ – CompanyDroneFromSector7G 2012-02-09 16:44:08