Reputation: 29
I want to dynamically load a javaScript file, and I think getScript is supposed to do that. But if so, what am I doing wrong?
I have testfile.js with a function test(). I try to load it with getScript instead of using <script src="testfile.js"></script>
. What am I doing wrong?
function test() {
console.log("passed");
}
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.getScript("testfile.js").done(function( script, textStatus ) {
console.log("loaded");
})
.fail(function( jqxhr, settings, exception ) {
console.log("failed");
});
test();
</script>
Upvotes: 1
Views: 148
Reputation: 29
JJJ correctly points out I was treating it as synchronous, but it's asynchronous, and I need to use the done callback. (JJJ was kind enough to not point out there'd be no point in the done callback if it was synchronous.)
Upvotes: 1