Creating custom JavaScript error dialog boxes
In this section, we'll look at how to create custom error
boxes that replace the default one in the event of a js error. This box,
unlike the default box, pops up only once, even if there are multiple errors
on the page:
<script type="text/javascript">
function errorbox(msg, url, linenumber){
var errwindow=window.open("","","width=300,height=300")
errwindow.document.write("<h3>A JavaScript error has occured</h3><br>")
errwindow.document.write('Error message= '+msg+'<br>URL= '+url+'<br>Line
Number= '+linenumber)
errwindow.document.close()
return true
}
window.onerror=errorbox
</script>
Click here to
see the above script used on a page containing js errors. Note: If
you browser uses a popup blocker, this example may not work for you.
There really is nothing new in the above script; it opens up
a new window, dynamically constructs a basic document inside of it to inform
the user of the js error(s), returns true inside function errorbox to cancel
the loading of the default error box, and that's that.
If you're really ambitious, you can actually create a custom
error box that, when closed, sends the information regarding the errors
(error message, line number etc) to you, providing you with an easy way to
debug your scripts with the help of your surfers. The idea is to store all
the error information into an array, and connect the form button inside the
error window to your usual form submission script, so all the error info is
submitted to you when the error box is closed. I'll help you get started by
writing the hardest part of such a script- storing the error messages into
an array:
<script type="text/javascript">
var. errorcontainer=new Array()
function reporterrorbox(msg, url, linenumber){
errorcontainer[errorcontainer.length]='Error message= '+msg+'\nURL=
'+url+'\nLine Number= '+linenumber
return true
}
window.onerror=reporterrorbox
</script>
Just so I can get some sleep, I'll leave the rest of the
code to you.
|