Bug Tracker

Modify

Ticket #11878 (closed bug: duplicate)

Opened 12 months ago

Last modified 12 months ago

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

Reported by: Alessandro Vernet <avernet@…> Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocking: Blocked by:

Description

  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

comment:1 Changed 12 months ago by dmethvin

#11879 is a duplicate of this ticket.

comment:2 Changed 12 months 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 12 months ago by dmethvin

  • Status changed from new to closed
  • Resolution set to duplicate

comment:4 Changed 12 months ago by dmethvin

Duplicate of #11795.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.