Skip to main content

Bug Tracker

Side navigation

#11879 closed bug (duplicate)

Opened June 08, 2012 01:08AM UTC

Closed June 08, 2012 01:33AM UTC

Last modified June 08, 2012 01:33AM 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 (3)

Changed June 08, 2012 01:10AM UTC by Alessandro Vernet <avernet@scdi.org> comment:1

Sorry folks; it turns out this got posted twice. So this is a duplicate of http://bugs.jquery.com/ticket/11878. Could someone close this bug?

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

resolution: → duplicate
status: newclosed

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

Duplicate of #11878.