0

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 ??

mathlearner
  • 7,509
  • 31
  • 126
  • 189

1 Answers1

-1

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.

collapsar
  • 17,010
  • 4
  • 35
  • 61
  • i added jQUery in head and using it in body!! – mathlearner Sep 04 '13 at 09:48
  • @RiteshMehandiratta: you should write so in your question (instead of downvoting, if that's been you). btw are you sure that you don't import jquery at the end of the body? have you checked the value of `x.src.indexOf('scripts/jquery-1.9.1.min.js')` ? – collapsar Sep 04 '13 at 10:15
  • yes i checked but it is not executing script only adding the child in head. i saw script tag of jquery in my dom – mathlearner Sep 04 '13 at 10:27
  • and apology if u mind downvoting next time i will keep this in mind – mathlearner Sep 04 '13 at 10:28
  • @RiteshMehandiratta: have you tried to eval the dom nodes textual contents in order to track down the problem ? does the error also occur with the non-minified flavor of the script or with another version ? btw, i don't mind downvoting in general, in this particular case i just felt there was no reason. – collapsar Sep 04 '13 at 13:09
  • i resolved the issue x.src='"'+rebasedUrl+'"'; in this line link is going in this way ""http://www.fnsfsjflsjfldss.com/fkshfdskh"" and due to second " the error was now every thing is working perfectly fine – mathlearner Sep 04 '13 at 13:17
  • @RiteshMehandiratta: congrats! – collapsar Sep 04 '13 at 13:22