Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross origin error handling #246

Open
szepeviktor opened this issue Jan 10, 2014 · 10 comments
Open

cross origin error handling #246

szepeviktor opened this issue Jan 10, 2014 · 10 comments

Comments

@szepeviktor
Copy link
Contributor

I've just read that window.onerror is not triggered if you use a CDN.
Please consider adding crossorigin="anonymous" to on-the-fly created script tags.

@unscriptable
Copy link
Member

Do you have a link to an article or specification that show how crossorigin="anonymous" can be used on script elements? I can only find information about using it on video and img elements. Thanks!

@szepeviktor
Copy link
Contributor Author

@unscriptable
Copy link
Member

Thanks for the link, @szepeviktor. From reading this article (and another from @briancavalier), it seems like we can only add this attribute when we know that the site serving the script has set Access-Control-Allow-Origin: *. Otherwise, the script does not evaluate.

Some background: scripts have never given any good error reporting, whether or not you use window.onerror or script.onerror or whether the script is remote or not. It'll be nice to get good errors so curl.js can provide something other than "HTTP or Syntax error". :)

This feels like an opt-in feature that would be specified on each remote script. Something like this:

curl.config({
    paths: {
        jquery: { location: "//cdn.jquery.com/jquery-2.0.3.min", crossorigin: "anonymous" }
    }
});

Thoughts?

-- John

@szepeviktor
Copy link
Contributor Author

It is very good.
It would be even nicer to have a global config option also.

curl.config({
    crossorigin: "anonymous",
    paths: {
        jquery: { location: "//cdn.jquery.com/jquery-2.0.3.min" }
    }
});

@unscriptable
Copy link
Member

All config options can be specified "globally" (although a few don't make sense to be used globally). This one, crossorigin, just seems like it's too error-prone to use it globally. :)

@szepeviktor
Copy link
Contributor Author

Do you plan to implement it?

@unscriptable
Copy link
Member

We're considering it. We don't yet know if async scripts gain any useful error information in their onerror events. Fwiw, a global window.onerror seems like the wrong place to send error info when there could be several, parallel scripts loading at once. I'm hoping that each script's onerror handler gets the info passed to it, as well.

Is this something you could test for us?

Also: it'd be very exciting if this feature prompted the IE team to start firing the script's onerror event, too. (Currently, IE never fires this event handler, which limits the features of script loaders as you can imagine!)

/me crosses fingers for an IE12 that doesn't suck. :)

@szepeviktor
Copy link
Contributor Author

I plan to report errors to Google Analytics on a small site.
I think there are no errors now. But it is good to track whether there are any.

Do you think a script loaded from CDN tells you more than "Script error on line 0" on error?

@szepeviktor
Copy link
Contributor Author

script.onerror does not fire:

window.onerror = function (msg, url, line) {
    console.log(msg)
    console.log(url + ':' + line)
    window.alert("win.onerror fires");
}

var head = document.getElementsByTagName('head').item(0);
var script = document.createElement('script');

script.src = '/f.js' // this contains: idontexist();
script.onload = function() {
    window.alert('this works');
}
script.onerror = function() {
    window.alert("onerror doesn't fire");
}
head.appendChild(script);

console.log('end.');

@szepeviktor
Copy link
Contributor Author

Do you plan to support crossorigin="anonymous" in the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants