I'm currently trying to report all uncatched JS errors like this with jquery:
window.onerror = function(msg, file, line) {
$.post("https://"+current_url+"/js_error", {
msg: msg
file: file
line: line
});
}
current_url
is always the domain I am currently at.
When I am at www.website.com
and the script is loaded from www.website.com/script.js
(same domain), everything works fine. I tried it with a undefined variable, and get the correct message variable is undefined
postet to https://www.website.com/js_error
.
Now, I have some subdomains: a.website.com
, b.website.com
etc. All of them have different content, but use exactly the same javascript.
My idea was the following: Always include the javascript from www.website.com
, so that when you switch the subdomain, the script can be cached by browsers and does not have to be redownloaded again.
But unfortunately this breaks the error reporting above. When the script is included from a different domain, e.g. I am at a.website.com
and include the script from www.website.com/script.js
, I only get these errors posted to https://a.website.com/js_error
(for firefox browsers):
"Script Error." on line 0
I realize this is due to the same-origin policy, see this question.
I also tried to hardcode current_url
to www.website.com
(from where the script is loaded), so the POST goes always there regardless of the domain I am at. But this POST does not work at all from a different subdomain than www.website.com
(I think because cross domain ajax POSTs are not possible).
I also tried to send the error as a GET ($.get
), but this always give me "Script Error." on line 0
on a subdomain - regardless the target of the GET.
So, how can I report errors for my script? I want to detect them so I can fix them, but do not want to give up the caching.
BTW: I am using firebug to debug my scripts, so I detect errors when I get them. But due to the complexity it is not always possible to spot every error for every OS/browser combination, and I want make sure to detect them when they happen at my clients, too.
EDIT: jsfiddle deleted