SReject
SReject

Reputation: 3936

Force the retrieval of a script file to block

Im working on a greasemonkey script, which injects a block of code into the page. That code then creates a script tag directed to a 3rd party source using the following:

function injectThis(){
    var s = document.createElement("script");
    s.src = "http://site.com/file.js";
    s.head.appendChild(s);

    // other code to be executed once the above file has finished downloading

}

Is there a way to make the engine block until the retrieval and processing of the script has completed?

A few notes:
1: A syncronized xhr is out of the question b/c it's cross domain
2: gm_xhr is out of the question due to the purpose of the script Im working on
3: Adding an event/callback in file.js to indicate loaded isn't a solution due to the same reason #2 isn't

Upvotes: 0

Views: 69

Answers (1)

LetterEh
LetterEh

Reputation: 26696

Why not listen to the onload of the script tag, and then fire your other stuff afterward?
If you need old-IE support, you can also listen for onreadystatechange.

The only way you're going to cause a synchronous load is by using .innerHTML or, much worse, document.write.

Much better would be to set script.onload = function () { callOtherStuff(); };.

Upvotes: 1

Related Questions