| home / programming / prof_ajax / 1 | [previous] |
|
|
Firefox, unsurprisingly, handles errors differently from IE. When IE runs into an error, it populates the
parseError object; when Firefox runs into an error, it loads an XML document containing the error into
the XML DOM document. Consider the following example:

In the highlighted line, you' see what will cause the error: a malformed XML string (because the
person/> element is not closed). When the malformed XML is loaded, the XML DOM object loads an
error document with a documentElement of <parsererror/>. You can easily determine if an error
occurred by checking the documentElement tagName property; if it’s not parsererror, you can be
assured that an error did not occur.
The error document created in this example looks like this:

All of the information about the error is available as text in the error document. If you want to use this information programmatically, you have to parse it first. The easiest way to do so is to use a rather lengthy regular expression:

This regular expression divides the error document into five sections: the error message, the file name
where the error happened, the line number, the position in the line where the error occurred, and the
source code that caused the error. Using the test() method of the regular expression object will enable
you to use these pieces of data:

The first chunk of data captured by the regular expression is error message, the second is the file name, the third is the line number, the fourth is the position in the line, and the fifth is source code. You can now use this parsed information to create your own error message:

If an error occurs, an alert box will display the relevant error information in an easy-to-read fashion.
In an Ajax application, and most JavaScript code, you always need to consider cross-browser differences. When using an XML-based solution in IE and Firefox, you have two options: create your own functions that use the correct code based on the browser, or use a ready-made library. Most of the time it’s easiest to use a pre-existing library, such as the zXml library introduced in Chapter 2. Along with XMLHttp support, zXml also has common interfaces for XML operations.
For example, to create an XML DOM document, you can use zXmlDom.createDocument():
var oXmlDom = zXmlDom.createDocument(); |
This single line of code can be used instead of doing separate browser-dependent code each time a DOM document is needed. Additionally, zXml adds a host of IE functionality to the standard Firefox DOM document.
One of the major things zXml does for convenience is to add support for the readyState property and
the onreadystatechange event handler. Instead of needing to use the separate onload event handler
in Firefox, you can write one set of code without browser detection, such as:

The zXml library also adds the xml and text attributes to all nodes in Firefox. Instead of using an
XMLSerializer or a standalone function to get these values, you can use them the same way as in IE:

zXml also provides a loadXML() method for the Firefox DOM document, eliminating the need to use a
DOMParser object.

Last, the zXml library adds a parseError object to the Firefox implementation. This object emulates
fairly closely the corresponding object in IE. The one major difference is the errorCode property, which
is simply set to a non-zero number when an error occurs. Therefore, you shouldn’t use this property to
look for a specific error, only to see if an error has occurred. Other than that, you can use the other properties
as you would in IE:

You certainly aren’t required to use a cross-browser XML library for your solutions, but it can definitely help. The following section develops an example using the zXml library.
Reproduced from "Professional Ajax" by Nicholas Zakas, Jeremy McPeak, Joe Fawcett, ISBN: 0471777781, published by WROX. Copyright 2006, Wiley Publishing Inc, used by permission of the publisher. All rights reserved.
| home / programming / prof_ajax / 1 | [previous] |
Created: March 27, 2003
March 6, 2006
URL: http://webreference.com/programming/prof_ajax/1