How to include jQuery library in Javascript without <script src=""> -
i need include jquery library in javascript file (john.js) remotely. have tried without luck;
(function(d, t) { var g = d.createelement(t), // create script tag s = d.getelementsbytagname(t)[0]; // find first script tag in document g.src = 'http://code.jquery.com/jquery-latest.js'; // set source of script script s.parentnode.insertbefore(g, s); // append script dom }(document, 'script')); $( document ).ready(function() { // jquery works here });
i want fetch script in javascript way. correct way ?
the error here jquery not loaded yet when code executed:
$(document).ready(function() { .. }
as is, error this: $ undefined
(or similar)
you should use onload
event of created script element sure loaded jquery.
this sample shown how can achieve goal. luck.
var newscript = document.createelement('script'); newscript.type = 'text/javascript'; newscript.src = 'http://code.jquery.com/jquery-latest.js'; // set source of script script newscript.onload = function() { alert("script ready!"); $(document).ready(function() { alert("jquery ready!"); }); }; var head = document.getelementsbytagname("head")[0]; head.appendchild(newscript);
Comments
Post a Comment