Welcome...



...All those moments will be lost in time, like tears in rain....

- soliloquy from Blade Runner


Bits and bytes do get lost, awash in the rain of data flow that is Internet. They slip away from us, never to found again... some of them will be captured here, many more will not... like tears in rain...



Tuesday, October 29, 2013

JavaScript error handling

 JavaScript does have try catch but it has something even handier for error catching - window.onerror.

 It used not to work on Chrome (yes, really!) and since now it works in pretty much all browsers, it's very convinient for error handling.

 Basically, it will redirect all console errors to your function.

 To get it to work, it has to be in its own script block (or it's own include file, the first one to be included).

<script type="text/javascript">
  window.onerror = function(msg, url, linenumber) {
        alert('Error message: ' + msg + '\nURL: ' + url + '\nLine Number: ' + linenumber);
}
</script>

Parameter (in order) Description
Error Message Contains a message explaining why the error occurred.
Error URL Contains the url of the page with the error script
Error Line Number Contains the line number where the error occurred

No comments:

Post a Comment