Markus
Markus

Reputation: 5807

How to report JS-Errors from Script at different domain?

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

Upvotes: 1

Views: 1240

Answers (1)

reconbot
reconbot

Reputation: 5287

CORS is exactly your problem, just add the CORS allowed headers to your https://"+current_url+"/js_error response.

Access-Control-Allow-Origin: *

Or to be more restrictive.

Access-Control-Allow-Origin: http://a.website.com http://b.website.com

http://enable-cors.org/ is a good resource for this.

As for the window.onerror not giving you all the data with errors from separate domains; I can confirm this isn't you being crazy and it's how firefox operates. http://jsbin.com/ojavoy/3/edit#javascript,html

  • Firefox leaves out the line number
  • Chrome doesn't give the script name or line number
  • IE <8 doesn't support window.onerror (don't know about 9)

I've seen people wrap their scripts in a bit try{}catch{} block. That will work cross browsers and will catch some but not all errors (which is better then none in IE). There currently isn't a "right way to do this".

Upvotes: 2

Related Questions