Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#9015 closed enhancement (invalid)

Add the possibility of synchronous loading of external scripts

Reported by: anonymous Owned by:
Priority: undecided Milestone: 1.next
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:

Description

https://github.com/jquery/jquery/blob/master/src/ajax/script.js

line 45:

script.async = "async";

replace to

script.async = s.async;

Change History (7)

comment:1 Changed 9 years ago by jaubourg

Resolution: invalid
Status: newclosed

script.async = false won't make the request synchronous in the $.ajax sense of the term. You'd end up having a script tag blocking other script tags while still having to deal with the request asynchronously.

To make it clear, there is no way to load a remote script synchronously unless you use a CORS-enabled XMLHttpRequest implementation.

comment:2 Changed 9 years ago by anonymous

OK, but when we have always script.async = "async"; this situation can arise:

Two scripts, one of which contains a function call is located in another. It is likely that the second script can be executed before the first. In this case we get an error.

And script.async = false; solves this problem.

comment:3 in reply to:  2 Changed 9 years ago by jaubourg

Replying to anonymous:

OK, but when we have always script.async = "async"; this situation can arise:

Two scripts, one of which contains a function call is located in another. It is likely that the second script can be executed before the first. In this case we get an error.

And script.async = false; solves this problem.

In browsers that support it...

comment:4 Changed 7 years ago by anonymous

Comment 2 is valid, though I think the proposed solution conflates the two notions of "async" here (async on the request is not the same as async on the script tag). Perhaps we need a different solution, but the issue is real.

My application breaks due to timing issues b/c of script.async = "async". Consider the following timeline of events:

Insert x-domain script 1 via JQuery. A script tag gets added to the HTML head with async="async" and then removed. Regardless of whether the browser has actually loaded and parsed the script, the success callback is called.

In the callback x-domain script 2 is inserted. If it depends on script 1, a javascript error occurs.

Due to async="async" being set on the script tag at insertion time, we can't depend on the ajax success callback to tell us if the work is done, dependencies satisfied, etc. Correct me if I'm wrong, but due to async="async" being set on the script tag, there is no way to do this with the current JQuery 1.7.2.

comment:5 Changed 7 years ago by dmethvin

Can't you just request the file as text using a sync XHR request and eval the result? This is a pretty bizarre specific need so it doesn't seem all that important for the general code path to handle it.

comment:6 Changed 7 years ago by anonymous

I don't think I can. In our production environment we serve scripts from a separate domain from our pages, so all of our script loading is seen as "cross-domain" and thus I can't just get the text and eval it (am I wrong about that?). I believe this is actually a reasonably common scenario.

comment:7 Changed 7 years ago by anonymous

Just for reference, looked at YUI 3. They appear to handle this. Perhaps the source comments in there are useful: http://yuilibrary.com/yui/docs/api/files/get_js_get.js.html#l3

Note: See TracTickets for help on using tickets.