Handling White Space in a Cross-Browser Way


You can write a function to strip white space from XML before working with that XML, which means your code will work with Internet Explorer as well as the Mozilla, Netscape, and Firefox browsers. To do that, you might create a function named removeWhitespace:

 function removeWhitespace(xml) {   .   .   . }

You can then loop over all nodes in the XML object passed to this function:

 function removeWhitespace(xml) {   var loopIndex;   for (loopIndex = 0; loopIndex < xml.childNodes.length;     loopIndex++) {     .     .     .   } }

Then you get an object corresponding to the current node in the loop:

 function removeWhitespace(xml) {   var loopIndex;   for (loopIndex = 0; loopIndex < xml.childNodes.length;     loopIndex++) {     var currentNode = xml.childNodes[loopIndex];     .     .     . }

If that current node is an element node (nodeType = 1), you should loop over all the children of the element, checking for white space nodes, which you can do by calling removeWhitespace again:

 function removeWhitespace(xml) {   var loopIndex;   for (loopIndex = 0; loopIndex < xml.childNodes.length;     loopIndex++) {     var currentNode = xml.childNodes[loopIndex];     if (currentNode.nodeType == 1) {       removeWhitespace(currentNode);     }     .     .     .   } }

If, on the other hand, you’re dealing with a text node (nodeType = 3), you have to check whether it’s a pure white space node. To do that, this code uses a regular expression to check whether the text node contains only spaces:

 function removeWhitespace(xml) {   var loopIndex;   for (loopIndex = 0; loopIndex < xml.childNodes.length;     loopIndex++) {     var currentNode = xml.childNodes[loopIndex];     if (currentNode.nodeType == 1) {       removeWhitespace(currentNode);     }     if (((/^\s+$/.test(currentNode.nodeValue))) &&       (currentNode.nodeType == 3)) {       .       .       .     }   } }

Note 

A full discussion on regular expressions is beyond the scope of this book. If you want all the details, take a look at http://perldoc.perl.org/perlre.html.

If you have found a pure white space node, you can remove it from the XML object with the removeChild method and decrement the loop index like this:

 function removeWhitespace(xml) {   var loopIndex;   for (loopIndex = 0; loopIndex < xml.childNodes.length;     loopIndex++) {     var currentNode = xml.childNodes[loopIndex];     if (currentNode.nodeType == 1) {       removeWhitespace(currentNode);     }     if (((/^\s+$/.test(currentNode.nodeValue))) &&       (currentNode.nodeType == 3)) {       xml.removeChild(xml.childNodes[loopIndex--]);     }   } }

That completes the removeWhitespace function. Now you have to pass the XML you got from the server to that function if you’re dealing with a Mozilla/Netscape/Firefox-type browser. To check whether you are dealing with that type of browser, you can set up a true/false variable named mozillaFlag in event.html:

 <html>   <head>     <title>Retrieving XML data</title>     <script language = "javascript">       function getData()       {         var mozillaFlag = false;         var XMLHttpRequestObject = false;         .         .         .

You can set the mozillaFlag variable to true if you’re dealing with a Mozilla/Netscape/Firefox-type browser, which you can check when you determine how to create the XMLHttpRequest object:

 <html>   <head>     <title>Retrieving XML data</title>     <script language = "javascript">       function getData()       {         var mozillaFlag = false;         var XMLHttpRequestObject = false;         if (window.XMLHttpRequest) {           XMLHttpRequestObject = new XMLHttpRequest();           mozillaFlag = true;         } else if (window.ActiveXObject) {           XMLHttpRequestObject = new             ActiveXObject("Microsoft.XMLHTTP");         }         .         .         .

And if you are working with a browser that needs to have white space removed from its XML documents, you can call the removeWhitespace function before going after the third guest:

 <html>   <head>     <title>Retrieving XML data</title>     <script language = "javascript">       function getData()       {         var mozillaFlag = false;         var XMLHttpRequestObject = false;         .         .         .         if(XMLHttpRequestObject) {           XMLHttpRequestObject.open("GET", "event.xml", true);           XMLHttpRequestObject.onreadystatechange = function()           {             if (XMLHttpRequestObject.readyState == 4 &&               XMLHttpRequestObject.status == 200) {             var xmlDocument = XMLHttpRequestObject.responseXML;             if(mozillaFlag){               removeWhitespace(xmlDocument);             }             displayThirdGuest(xmlDocument);             }           }           XMLHttpRequestObject.send(null);         }       }

That lets you handle the white space issue in a cross-browser manner.



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

Similar book on Amazon
HTML, XHTML, and CSS Bible
HTML, XHTML, and CSS Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
JavaScript Bible
PHP and MySQL Web Development (4th Edition)
PHP and MySQL Web Development (4th Edition)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net