Focusing on one of the accepted answer's jQuery solutions, $.getScript()
is an .ajax()
request in disguise. It allows to execute other function on success by adding a second parameter:
$.getScript(url, function() {console.log('loaded script!')})
Or on the request's handlers themselves, i.e. success (.done()
- script was loaded) or failure (.fail()
):
$.getScript(_x000D_
"https://code.jquery.com/color/jquery.color.js",_x000D_
() => console.log('loaded script!')_x000D_
).done((script,textStatus ) => {_x000D_
console.log( textStatus );_x000D_
$(".block").animate({backgroundColor: "green"}, 1000);_x000D_
}).fail(( jqxhr, settings, exception ) => {_x000D_
console.log(exception + ': ' + jqxhr.status);_x000D_
}_x000D_
);
_x000D_
.block {background-color: blue;width: 50vw;height: 50vh;margin: 1rem;}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div class="block"></div>
_x000D_