jquery - How to defer javascript -
i using fancybox jquery plugin , trying defer javascript end of page loading
<html> <head> </head> <body> content /*bottom of body*/ <script src="jquery-1.8.3.min.js" type="text/javascript"></script> <script type="text/javascript" src="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js" ></script> <script type="text/javascript"> $(document).ready(function() { $(".example3").fancybox({ 'transitionin' : 'none', 'transitionout' : 'none' }); }); </script> </body> </html> at point plugin works javascript not deferred. change script to
<script defer="defer" type="text/javascript"> window.onload=function(){ var mycode; mycode=document.createelement("script"); mycode.type="text/javascript"; mycode.src="jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js"; document.getelementsbytagname("head")[0].appendchild(mycode); } </script> like read here, plugin work, doesn't work. according google pagespeed still need defer jquery-1.8.3.min.js script. if defer same way, google pagespeed seems ok it, plugin doesn't work @ all.
how supposed this?
complete solution based on jamed donnelly's answer:
i removed .js scripts head. added before tag:
<script> (function() { function getscript(url,success){ var script=document.createelement('script'); script.src=url; var head=document.getelementsbytagname('head')[0], done=false; script.onload=script.onreadystatechange = function(){ if ( !done && (!this.readystate || this.readystate == 'loaded' || this.readystate == 'complete') ) { done=true; success(); script.onload = script.onreadystatechange = null; head.removechild(script); } }; head.appendchild(script); } getscript("jquery-1.8.3.min.js",function(){ getscript("jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js", function() {}); getscript("mycustomscript.js", function() {}); }); })(); </script> i created file mycustomscript.js:
$(document).ready(function(){ $(".example3").fancybox({ 'transitionin' : 'none', 'transitionout' : 'none' }); }); and that's all.
what i've been using while is:
(function() { function getscript(url,success){ var script=document.createelement('script'); script.src=url; var head=document.getelementsbytagname('head')[0], done=false; script.onload=script.onreadystatechange = function(){ if ( !done && (!this.readystate || this.readystate == 'loaded' || this.readystate == 'complete') ) { done=true; success(); script.onload = script.onreadystatechange = null; head.removechild(script); } }; head.appendchild(script); } getscript("whatever1.js",function(){ getscript("whatever2.js", function() {}); }); })(); i'll have dig try , find got from.
edit: no luck on finding source of this, it's similar this article on css tricks.
in case you'd like:
getscript("jquery-1.8.3.min.js",function(){ getscript("jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack.js", function() {}); getscript("mycustomscript.js", function() {}); }); where mycustomscript.js file containing document ready handler. loads jquery, once that's loaded loads plugin , script.
Comments
Post a Comment