Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11878 closed bug (duplicate)

When using load(url…), some scripts are loaded synchronously, others asynchronously

Reported by: Alessandro Vernet <[email protected]…> Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:


  1. .load() calls .html(), which calls .append(), which calls .domManip()
  2. .domManip() calls .buildFragment(), which extracts the scripts from the HTML
  3. Then .domManip() loads the scripts in a loop calling evalScript().
  4. For <script src="…">, evalScript() calls .ajax() with the datatype set to script.
  5. Then if the script doesn't require a cross-domain request, it is loaded with Ajax synchronously. If it does require an Ajax request, it is loaded by adding a script element to the DOM, asynchronously.

As a result, if the HTML contains a mix of cross-domain and same-domain request, some scripts are loaded synchronously, while others are loaded asynchronously. This has a number of undesirable effects:

  1. Code assuming scripts are loaded synchronously, as would be done by the browser if the HTML was loaded directly, might fail. Imagine a page loading OpenLayers (cross-domain) and "then" a script using OpenLayers (same domain), where that script could run before OpenLayers is loaded.
  2. The callback on .load() might be called before some scripts have loaded.

It is easy enough to get jQuery to load all the scripts asynchronously, in the order they are specified. See this patch: https://gist.github.com/2892778.

However, I'd also like .load() to only be called once all the scripts have been loaded, and I don't have a very good solution for that. Adding callbacks to the signature of .html(), .append(), and .domManip() feels a bit intrusive to me. Hopefully someone has a better suggestion.

Change History (4)

comment:1 Changed 11 years ago by dmethvin

#11879 is a duplicate of this ticket.

comment:2 Changed 11 years ago by dmethvin

I agree with your analysis, but am not sure about a solution.

To me it seems like a bad idea to pass a bunch of script inside arbitrary HTML and expect it to be processed consistently. We don't have a way to process cross-domain script requests synchronously in any case. I'm against making a wide swath of existing APIs more complex for this one case, but making everything async is a backcompat issue too.

I'll close this as a dup of #11795 but see also #11743 and #11617 which might help build a plugin to handle special cases.

comment:3 Changed 11 years ago by dmethvin

Resolution: duplicate
Status: newclosed

comment:4 Changed 11 years ago by dmethvin

Duplicate of #11795.

Note: See TracTickets for help on using tickets.