Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:<;脚本src=jquery…仅当条件为true时_Javascript_Jquery_Get_Conditional Statements - Fatal编程技术网

Javascript:<;脚本src=jquery…仅当条件为true时

Javascript:<;脚本src=jquery…仅当条件为true时,javascript,jquery,get,conditional-statements,Javascript,Jquery,Get,Conditional Statements,我正在用javascript开发,只想在条件得到验证的情况下插入脚本 例如: var a = exampleVariable; if (a == conditionIwant) { // append to head: <script src="http://code.jquery.com/jquery-1.5.js"> </ script> }; //or something like this var a=example变量; if(a==co

我正在用javascript开发,只想在条件得到验证的情况下插入脚本

例如:

var a = exampleVariable;

if (a == conditionIwant) {
    // append to head: 
    <script src="http://code.jquery.com/jquery-1.5.js"> </ script>
};  //or something like this
var a=example变量;
if(a==conditionIwant){
//附加到标题:
};  //或者类似的事情
如果条件为真,我如何才能插入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);
}

您是否可以访问其他技术—PHP、ASP等。如果可能,这确实是您应该在服务器端执行的操作。最终您可以添加
sc.async=true如果不想阻止并行下载。