Triggering window.onerror

Ajaxian covers a post on logging client-side errors on the server-side using AJAX. A commenter made mention of inconsistent behaviour when using the onerror event as detailed on PPK.

In determining the issue, I noticed what appeared to be the issue and left the following comment on Ajaxian:

For example, trying to run myfunction() where myfunction doesn't exist will trigger onerror. But running myfunction( where there's a missing bracket will not trigger it.

In actuality, upon further testing, this seems to be incorrect. PPK's test case definitely demonstrates something peculiar with the onerror but in any of the test cases I created, I was able to trigger onerror whether it was a syntax or runtime error. My suspicion at this point lies in the fact that PPK's test case expects the error handler to pass in the event object. It would appear that onerror actually passes in the error message, file name and line number as parameters instead.

Published April 12, 2006 · Updated September 14, 2006
Categorized as JavaScript
Short URL: https://snook.ca/s/572

Conversation

2 Comments · RSS feed
Martin said on April 12, 2006

That should rather read "as detailed on quirksmode.org by PPK" :-)

Justin Palmer said on April 13, 2006

Your absolutely right. onerror doesn't pass the event object and instead passes the msg, url, and line number.

Also note for those using Prototype, Event.observe will not correctly observe onerror. When I was writing logger.js (http://encytemedia.com/treasure/file/javascript/logger/logger.js) I had to use window.onerror instead.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.

Want to learn about scaling CSS for large projects?

I'm available for full and half-day workshops on scalable CSS architecture. I can provide on-site training for your team. Interested?
Get in touch.