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>
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