Javascript:<script src = jquery …仅当condition为true时

我正在开发javascript,并且只有在条件得到validation时才想插入脚本。

例如:

var a = exampleVariable; if (a == conditionIwant) { // append to head:   }; //or something like this 

如果条件为真,我怎么能插入jquery.js?

这很简单:

 if(somethingIsTrue) { var sc = document.createElement('script'); sc.src = 'http://code.jquery.com/jquery-1.5.js'; sc.type = 'text/javascript'; if(typeof sc['async'] !== 'undefined') { sc.async = true; } document.getElementsByTagName('head')[0].appendChild(sc); }