jquery / jquery/api.jquery.com

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

Open
#420 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
HTML
Stars
325
Forks
260
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the getScript documentation and the referenced src/ajax/script.js converter, then reproduce the supplied test in Firefox and Chrome. Compare the observed callback timing with the documented warning and identify the specific conditions, if any, where it applies. Done means the documentation accurately explains the behavior, with supporting tests or references.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jquery
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.