52 Part I: Core Ideas
You could then bind this error handling library to arbitrary Web pages, which may or may
not have errors, as shown here:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Chapter 2 : JavaScript Error Reporting Demo</title>
<script src=" http://ajaxref.com/ch2/ errorreporter.js"
type="text/javascript"></script>
<script type="text/javascript">
/* scripts that may trigger errors */
function badCode()
{
alert("Good code running when suddenly... ");
abooM("bad code! "); /* BAD CODE ON PURPOSE */
}
</script>
</head>
<body>
<h3>JavaScript - Silent Errors, Deadly Errors</h3>
<form action="#">
<label>Do you dare press it?
<input type="button" value="BOOM!" onclick="badCode();" />
</label>
</form>
<br /><br />
<a href="http://ajaxref.com/ch2/jserror.txt">See error file</a>
</body>
</html>
You get a sense of what might happen if an error is caught in Figure 2-10.
CCAU T IONAUTION The window.onerror mechanism is not supported in all Web browsers, notably Safari
2.0.x and Opera 9.x and before. Hopefully this useful feature will be added in future releases.
The server-side code for recording client errors is fairly similar to the ratings example,
but this time we collect a bit more data, such as the browser used as that may be helpful
during debugging. It can be found online and is omitted for space.
Readers should consider themselves warned that using such error tracking scripts in
projects will likely expose them to the vast number of JavaScript errors that are triggered by
visitors due to edge cases, browser support, and just plain sloppy coding. Sadly, after seeing
this to be true, you might truly feel ignorance to be bliss.
Offsite Link Press Monitoring
Another interesting use of the one-way communication technique is checking for offsite link
clicks by the user. Commonly sites will annotate their outbound links to bounce off of
themselves. For example, the link <a href="http://ajaxref.com/bounce.php?site=
pint.com">PINT</a> calls the file “bounce.php,” which then records the user’s choice