Skip to main content

Bug Tracker

Side navigation

#11878 closed bug (duplicate)

Opened June 08, 2012 01:07AM UTC

Closed June 08, 2012 01:58AM UTC

Last modified June 08, 2012 01:58AM UTC

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

Reported by: Alessandro Vernet <avernet@scdi.org> Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:
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.

Attachments (0)
Change History (4)

Changed June 08, 2012 01:33AM UTC by dmethvin comment:1

#11879 is a duplicate of this ticket.

Changed June 08, 2012 01:58AM UTC by dmethvin comment:2

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.

Changed June 08, 2012 01:58AM UTC by dmethvin comment:3

resolution: → duplicate
status: newclosed

Changed June 08, 2012 01:58AM UTC by dmethvin comment:4

Duplicate of #11795.