For my backend I want to automatically load javascript files when it detects certain elements. Here is an example:
if($('.wysiwyg').length>0) { include('javascript/ckeditor/ckeditor.js'); $(".wysiwyg").ckeditor(); } But when I execute the code I get $(".wysiwyg").ckeditor is not a function because it seems the browser is still loading or parsing the javascript file that was included on the line before. If I put an alert popup right before the function it does work because it "pauzes" the script I guess and gives it time to load the file.
Is there a way I can know when the file is actually loaded so that the followed code can be executed?
EDIT: Seems that I asked this question a bit too soon. I found out the e.onload property for a callback function that solved this problem. This is my function now if others might stumble upon the same problem:
function include(script, callback) { var e = document.createElement('script'); e.onload = callback; e.src = script; e.type = "text/javascript"; document.getElementsByTagName("head")[0].appendChild(e); } if($('.wysiwyg').length>0) { include('javascript/ckeditor/ckeditor.js', function() { $(".wysiwyg").ckeditor(); }); }