50 Part I: Core Ideas
Example Applications
Before wrapping up the discussion of traditional JavaScript remote communication, we
present a few useful applications of the one-way and two-way techniques covered.
A Client-Side JS Error Reporter
Given the value of reporting user error conditions, we might explore writing an error
handler that communicates back to our server using the one-way pattern, showing how
many errors users are experiencing client-side. When JavaScript’s onerror handler for the
Window object gets called, it will create an image request reporting the situation to a server-
side program. We present an example JavaScript error reporter as a .js file with name
spacing used heavily so it can easily be included in arbitrary files.
/* object literal wrapper to avoid namespace conflicts */
var AjaxTCRExamples = {};
/* URL of your server-side error recording script */
AjaxTCRExamples.errorReportingURL =
"http://ajaxref.com/ch2/setjavascripterror.php";
AjaxTCRExamples.encodeValue = function(value)
{
var encodedVal;
if (!encodeURIComponent)
FIGURE 2-9 Graphical over view of JavaScript-based communication