在外部javascript文件中加载jquery

时间:2013-10-21 18:55:59

标签: 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(){
       $(function(){
         //jquery code here
  });
 }

如果不调用函数 init() onload,则会出现$ reference错误。   任何其他更好的方法,然后建议我。感谢

1 个答案:

答案 0 :(得分:0)

请尝试使用getScript

http://api.jquery.com/jQuery.getScript/

$.getScript( "external.js" )
  .done(function( script, textStatus ) {
    init();
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log("Triggered ajaxError handler.");
});
相关问题