Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to report JS-Errors from Script at different domain?
    text
    copied!<p>I'm currently trying to report all uncatched JS errors like this with jquery:</p> <pre><code>window.onerror = function(msg, file, line) { $.post("https://"+current_url+"/js_error", { msg: msg file: file line: line }); } </code></pre> <p><code>current_url</code> is always the domain I am currently at.</p> <p>When I am at <code>www.website.com</code> and the script is loaded from <code>www.website.com/script.js</code> (same domain), everything works fine. I tried it with a undefined variable, and get the correct message <code>variable is undefined</code> postet to <code>https://www.website.com/js_error</code>.</p> <p>Now, I have some subdomains: <code>a.website.com</code>, <code>b.website.com</code> etc. All of them have different content, but use exactly the same javascript.</p> <p>My idea was the following: Always include the javascript from <code>www.website.com</code>, so that when you switch the subdomain, the script can be cached by browsers and does not have to be redownloaded again.</p> <p>But unfortunately this breaks the error reporting above. When the script is included from a different domain, e.g. I am at <code>a.website.com</code> and include the script from <code>www.website.com/script.js</code>, I only get these errors posted to <code>https://a.website.com/js_error</code> (for firefox browsers):</p> <pre><code>"Script Error." on line 0 </code></pre> <p>I realize this is due to the same-origin policy, see <a href="https://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox">this question</a>.</p> <p>I also tried to hardcode <code>current_url</code> to <code>www.website.com</code> (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 <code>www.website.com</code> (I think because cross domain ajax POSTs are not possible).</p> <p>I also tried to send the error as a GET (<code>$.get</code>), but this always give me <code>"Script Error." on line 0</code> on a subdomain - regardless the target of the GET.</p> <p>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.</p> <p>BTW: I am using firebug to debug my scripts, so I detect errors when <em>I</em> 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.</p> <p><strong>EDIT</strong>: jsfiddle deleted</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload