AJAX Thoughts, Suggestions and Examples Part III

Special Characters: Since XML is the format which our data is transmitted, some key characters will need to be escaped. The ones that I am aware of are "<", ">" and "&". There may be more, however, from my testing all other characters on a United States standard keyboard seem to be fine. If I am incorrect, please let me know.

There are a few ways this could be done. We could use javascripts escape function to encode our xml values, but then on the server side, we need to ensure we are unescaping all of the values which are passed. I personally don't like this way as well, and we would still need to unescape these characters when we receive XML from the server.

I have made two very simple javascript functions to XML encode and XML decode our data. They are shown below integrated with the other functions described next, getText() and getNodeText(). The getText() function has two parameters, the XML to be parsed and the node of which to retrieve data. The getNodeText() function has a single parameter, the XML node which data should be retrieved. To use them, simply call the appropriate function from the callback procedure. These functions should also be in a convenient reusable javascript file to allow for ease coding and re-use. The xmlDecode() function never need be called from anywhere except the getText() and getNodeText() functions as shown below. The xmlEncode() function will need to be called whenever the XML data to be sent to the server is constructed.

Browser Compatibility: If you have read any basic articles on AJAX coding, you will notice that the most pointed out browser incompatibility is with the HTTP Request Object and many examples are given on making this cross browser compatible, but I want to talk about the more in depth issues I have seen. Once again I've seen so-called "professional" programmers use AJAX, but their code only works for a single browser (yes, the evil, but most widely used one). Good old Microsoft uses some proprietary XML object attributes for parsing xml such as the xml and text attributes. These work quite well if using IE of course, but do not work with any other browsers. The above code has been tested and does work on IE6+, Firefox 0.8+, Netscape 7.0+ and Safari for Mac. It has not been tested thoroughly, but should also work on Opera 6.0+. Other browser testing has not been attempted, it may or may not work.

This completes my ramblings on AJAX for now. If I come up with anything else in the future, I may post it. Once again, I will disclaim, improvements can probably be applied to my code examples, but they have been tested and used in real world environments with great success.

Go Back to Part II here