Background
I download and execute a script from another server ( cross-domain ) via jQuery .ajax(...) call.
There is some code that needs to be executed after the code from another server was executed , because otherwise some objects are "undefined" for now.
Perhaps important: The remote code contains another call to getScript(...) . And I have to wait until this code is executed. I cannot just load this second script from my code because its source is dynamic (i.e. Depends on some results of the remote script).
Doesn't work: success callback
Apparently, the success callback is called after the remote code barked, but before the remote code was executed .
# coffee script executeLater = ->
Doesn't work: async: false
Apparently, the async attribute is ignored for cross-domain queries, as stated here in the jQuery documentation: http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
In addition, I would like to avoid installing async: false , as it is said to block the browser.
# coffee script executeLater = ->
Doesn't work: $.when(...).then(...)
Using jQuery once upon a time mechanism , apparently the then code is executed before when the block is executed .
# coffee script executeLater = ->
EDIT: Worked but unusable: ajax both scripts
I cannot do this in production, as I said in the "background" section above, but if I reduce all cases to one and load the second script, which is usually executed by the remote script, everything works fine in my script.
# coffee script executeLater = ->
What to avoid
I would really like to use constructs like the setTimout pairs until a specific object is defined using the executeLater() method.
What I need: executed callback
It would be ideal to use the executed callback rather than the success callback of the ajax method. But so far I have not found this callback.
# coffee script executeLater = ->
Any clues?
Does anyone know how I can execute the executeLater method after the remote code has been executed ? Thanks!
EDIT: single-origin policy
As adeneo noted in the comment section, JavaScript can be a policy issue of the same origin .
a script that loads with an ajax or getScript is not allowed to download and execute another script from a remote server in order to prevent a malicious script from the "calling home",
This is confirmed by the following experiment:
This does not work:
<html><head> <script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script> <script language="javascript"> jQuery.getScript("http://example.com/script-from-other-server.js") </script> </head><body></body></html>
It works:
According to this answer stackexchange, a policy of the same origin allows remote scripts loaded with the html <script> to load other remote scripts via ajax .
<html><head> <script language="javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script> <script language="javascript" src="http://example.com/script-from-other-server.js"></script> </head><body></body></html>
The question remains : is there a good way to do this with an ajax call, or do I need to "prove" that I "own this code" by inserting the <script> in the html document?