Uncaught ReferenceError: $ is not defined

1.3k views Asked by At

i am trying to create a google gadget which should be built on javascript.every google gadget create a iframe element.due to some constraints i can't add jquery directly from script tag.i have to include it using javascript so i write a code

function addScript(jsRelativeUrl) {
  var rebasedUrl = rebaseRelativeUrl(jsRelativeUrl,true);

 var x = document.createElement('script');
 x.type='"text/javascript"';
 x.src='"'+rebasedUrl+'"';
if(x.src.indexOf('scripts/jquery-1.9.1.min.js')>=0)
 document.head.appendChild(x)
else document.body.appendChild(x);

}

i am first calling this function

gadgets.util.registerOnLoadHandler(function(){
  addScript('scripts/jquery-1.9.1.min.js');
     $(function (){});
});

but it is giving me error

 Uncaught ReferenceError: $ is not defined 

and when i see in elements jQuery is included in head tag of iframe . can any one please help why i am getting this error and how to getrid of this ??

1

There are 1 answers

7
collapsar On

You are appending the script tag to load jquery at the end of either the head or the bodyof the embedding document. if the code you are executing is situated before the location of the jquery import tag (eg. in a script section of the head or - if appended to the end of the body- in a scriptsection within the body), it cannot be present.