bostonquad
bostonquad

Reputation: 29

What's wrong with my use of jQuery's getScript?

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

Answers (1)

bostonquad
bostonquad

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

Related Questions