Tgr
Tgr

Reputation: 28190

Log javascript errors from another subdomain

window.onerror in Firefox and Chrome seems to discard the real error message/location and always pass "Script error.", "", 0 when the offending script is on a different domain than the page itself. I have a site with separate www and static subdomains for pages and css/js, which renders error logging rather useless. Is there any way to enable the proper logging of such errors?

Upvotes: 3

Views: 253

Answers (3)

N Rohler
N Rohler

Reputation: 4615

The real solution is to use proper try { ... } catch(e) { ... } blocks in all code. However, I understand that may not always be an option.

If you don't have control over these other scripts, your next best option would be to load them as strings via JSONP, then use eval() (yes, I know eval is evil) to "inject" them into the current page. This way you'll still get the benefits of using a static domain (no cookies, CDN option, additional concurrent requests, etc), but the JS will end up being on the page's request domain.

Upvotes: 1

broofa
broofa

Reputation: 38132

It doesn't sound like there's a workaround for this at the moment (I asked in the Mozilla IRC channel).

I've filed these bug reports to track this issue. Please chime in if you have opinions on what the best solution to this would be:

https://bugzilla.mozilla.org/show_bug.cgi?id=696301

https://bugs.webkit.org/show_bug.cgi?id=70574

Upvotes: 2

Naren
Naren

Reputation: 1920

Within your js file, on the top try doing

document.domain = "yourdomain.com"

Upvotes: 0

Related Questions