Skip to content
Advertisement

Synchronizing loading js files with ajax calls and loading js files with tag

core.js:

JavaScript

me.js

JavaScript


CASE 1:

JavaScript

CASE 2:

JavaScript

The problem is that for case 1 I get an alert, as I should , but for case 2, no alert…

So the question is: there is a load event for the <script> tag? Qhat can I use to synchronize the files to work on case 2 (while debugging in IE8, I noticed that using a breakpoint solves the problem)? Is there anything I’m missing?

Advertisement

Answer

I am not sure why CASE 1 happened to work for you, but your problem seems to lie here:

JavaScript

“this” in javascript isn’t the same as “this” in C++ or Java

The problem is that function(data){...} creates a closure (with a new this). So, inside that function, this no longer refers to core, it refers to the scope of the context from which it was called (somewhere inside jQuery in this case). You can fix it by protecting this with a new variable, like so.

JavaScript

This question can point you in the right direction for demystifying the this keyword in javascript.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement