I have the following code which loads an external script and is supposed to execute some code when it’s loaded:
$('<script/>', {
type: 'text/javascript',
src: 'https://raw.github.com/einars/js-beautify/master/beautify.js'
}).on('load', function() {
alert('jsb loaded' + typeof js_beautify);
}).appendTo('body');
However, the event never fires – even though the script is properly loaded, as verified with
window.setTimeout(function() {
alert(typeof js_beautify);
}, 1000);
which alerts function just fine.
You should call
$.getScript(), which does exactly that, and works correctly.