Skip to content

getScript docs have a possibly misleading warning about success resolving before execution #420

Open
@albell

Description

@albell

The getScript docs say about the success callback that:

"The callback is fired once the script has been loaded but not necessarily executed."

But in my testing that doesn't seem to be true. For a host page with:

var startTime = new Date();

$.getScript("test.js")
 .done(function( script, textStatus ) {
    console.log( textStatus );
    console.log( "Done callback executing now.")
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log("error." );
});

loading the following "test.js" script which ties up the UI for 5 seconds:

console.log("ajaxed script starting to execute.");
var newTime = new Date();
while (newTime - startTime < 5000) {
    newTime = new Date();
}
console.log("elapsed time", newTime - startTime);
console.log("ajaxed script finished executing.");

results in the same predictable console output in both FF & Chrome:

ajaxed script starting to execute.
elapsed time 5000 
ajaxed script finished executing.
success
Done callback executing now. 

In other words, the success callback does not ever fire until the loaded script is both loaded and executed. This seems to be because in the source, the globalEval function is calling the script immediately:

converters: {
    "text script": function( text ) {
        jQuery.globalEval( text );
        return text;
    }
}

So are the docs wrong? If they are correct, then in what specific cases will the success callback fire before the script is executed?

(question moved from StackOverflow)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions