Side navigation
#9015 closed enhancement (invalid)
Opened April 29, 2011 12:25PM UTC
Closed April 29, 2011 01:58PM UTC
Last modified July 03, 2012 09:56PM UTC
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;
Attachments (0)
Change History (7)
Changed April 29, 2011 01:58PM UTC by comment:1
resolution: | → invalid |
---|---|
status: | new → closed |
Changed April 29, 2011 04:08PM UTC by comment:2
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.
Changed April 30, 2011 01:06AM UTC by comment:3
Replying to [comment:2 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...
Changed July 03, 2012 09:02PM UTC by comment:4
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.
Changed July 03, 2012 09:06PM UTC by comment:5
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.
Changed July 03, 2012 09:11PM UTC by comment:6
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.
Changed July 03, 2012 09:56PM UTC by comment:7
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
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.