Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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文件中加载jquery_Javascript_Jquery - Fatal编程技术网

在外部javascript文件中加载jquery

在外部javascript文件中加载jquery,javascript,jquery,Javascript,Jquery,我想在外部js文件中加载jquery并使用它。 我在test.js中按照下面给定的方式做这件事 var src='http://code.jquery.com/jquery-1.10.1.min.js'; document.write('<sc'+'ript type="text/javascript" src="'+src+'" onload="init()"> </sc'+'ript>'); function init(){ $

我想在外部js文件中加载jquery并使用它。 我在test.js中按照下面给定的方式做这件事

   var src='http://code.jquery.com/jquery-1.10.1.min.js';
   document.write('<sc'+'ript type="text/javascript" src="'+src+'" onload="init()">     </sc'+'ript>');

 function  init(){
       $(function(){
         //jquery code here
  });
 }
var src='1〕http://code.jquery.com/jquery-1.10.1.min.js';
文件。写(“”);
函数init(){
$(函数(){
//这里是jquery代码
});
}
如果不调用函数init()onload,我将收到$reference错误。
还有其他更好的方法吗,那就给我提个建议。谢谢

尝试改用
getScript


ReferenceError:$未定义$.getScript(“”)猜您没有正确理解我的问题。我想在我的外部js中使用jquery。e、 g在test.js中您想使用jQuery库中的
getScript
来加载jQuery库吗?我们如何在不加载jQuery文件的情况下使用getScript??我们需要先加载jquery,然后才能使用$variable。那么如何做到这一点呢?不能使用jquery加载jquery。在包含jquery之前,您将无法访问
$
变量。这个问题可能有一个选项,你可以使用阅读我的问题,请。我知道我不能使用jquery加载jquery。我用过javascript,你能推荐其他更好的使用javascript的方法吗?
$.getScript( "external.js" )
  .done(function( script, textStatus ) {
    init();
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log("Triggered ajaxError handler.");
});