Thursday 15 August 2013

jquery - Javascript - ReferenceError: $ is not defined -



jquery - Javascript - ReferenceError: $ is not defined -

so have web service working on, error "referenceerror: $ not defined". know you're thinking, wrong order of loading jquery. well, here code below load scripts. tooltip.js file containing javascript/jquery, , error beingness thrown.

function loadjscssfile(filename, filetype, level){ if (filetype=="js"){ //if filename external javascript file var fileref=document.createelement('script') fileref.setattribute("type","text/javascript") fileref.setattribute("src", filename) } parent.document.getelementsbytagname(level)[0].appendchild(fileref) } window.onload = function() { loadjscssfile("//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", "js", "head"); loadjscssfile("//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css", "css", "head"); loadjscssfile("//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js", "js", "head"); loadjscssfile("...tooltip.js", "js", "head"); }

now in next code error beingness thrown. beingness thrown on shown, $(document)... beingness called. phone call preceded plain javascript , followed jquery. in tooltip.js.

//some javascript functions $(document).ready(function() { ... //some jquery functions ... }

i have tried can think of (which isn't much, beingness beginner @ both javascript , jquery), , have searched on find solutions, many of appear ordering of script loading.

the real kicker me works fine, other times seems jquery doesn't load (hence referenceerror). have ideas?

the alternative, of course, re-write entire script in javascript, far want do. please allow me know if need farther info, code, or explanation.

edit: problem though attaching the js files in order want them loaded in, not beingness loaded in order. solution have unload method loading query, , in method set loading other files. , seems work.

you right order of jquery beingness loaded wrong. here's why:

the ready event fires html document ready while onload event waits , fires after content (images, etc...) has loaded.

so problem due onload firing after ready. note in theory, onload should fire after ready.

i set js libraries in head of page create sure available. people might argue js should @ bottom of page, take on it's own judgement so. having js @ bottom has pros , cons, depending on situation.

javascript jquery

No comments:

Post a Comment