I’m using Google hosted jQuery in my webapp (//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js) As part of bug diagnostics I have a window.onerror handler which catches any errors I’m not catching locally and lets the server know about them.
So far so good, but… sometimes i get errors like these:
“Script error.”,”Error loading script”,”Unexpected token <“
My assumption is that the Google CDN is blocked in these cases (for whatever reason). I do have a local fallback for jQuery, that I’m fairly sure is working well, but I would like to find out what’s being returned so that I can test my assumptions and maybe get some of these users on a white list for Google CDN (if it’s company firewall blocking it).
But so far I haven’t been able to figure out how to retrieve the returned content. Can’t retrieve innerText of a SCRIPT tag if it’s a file, can’t do an ajax request because of cross-domain policy, etc.
Does anyone have any ideas about how this would be possible?
It simply isn’t possible to get the content of any file referenced by a
<script>tag. This is with good reason: doing so would allow you to circumvent XHR’s Same Origin Policy.Consider:
If you could access the text of the respnse, you’d be able to do this:
That’s obviously bad. Therefore, you’re never allowed to read the content of something brought in by
<script>tags.Your particular situation is complicated by a relatively recently introduced change where errors in cross-origin scripts do not report any useful information to your page’s
onerrorhandler. (Essentially, this was done to patch an information disclosure security hole that allows a malicious site to infer whether you’re logged in to some well-known sites, among other things.)This means that you get no useful information about errors from CDN-hosted script, so another change was made to allow the use of CORS for a CDN (or other non-same-origin) server to opt in to allowing full error details to pass to an
onerrorhandler.The
crossoriginattribute (originally intended for<img>) allows you to specify that a resource should be loaded with CORS rules. It has been implemented by Mozilla, WebKit, and Chrome.Unfortunately for you, in my testing, I found that the Google CDN does not send CORS headers.
Note the presence of the
Originheader in the request (indicating a CORS request), and the absence of anAccess-Control-Allow-Originheader in the response. Thus, even if you put thecrossoriginattribute, the CORS check will fail, and your scripts will receive scrubbed error details.There is a three-year-old issue to enable CORS on the Google CDN server. I wouldn’t hold my breath.
tldr: If you want meaningful error messages, you must host all JavaScript yourself, on the same origin.