|
|
|||||
Home » Tutorials » How to resolve browser compatibility issues while working with JavaScript, AJAX and XML
How to resolve browser compatibility issues while working with JavaScript, AJAX and XMLAfter going through this article, you will get an idea on resolving browser compatibility issues while working on JavaScript, AJAX and XML. I think, most of the developers might have faced browser compatibility issues while working with AJAX and XML, by assuming XML as a back-end database to retrieve respective data depending the events or requests sent by the user. When we run an application which you develop using AJAX and XML it behaves differently in different browsers like mostly in IE (Internet Explorer) and Mozilla Firefox.
What is AJAX? What is JavaScript? JavaScript is a Scripting Language used for client-side web development, this is also well known as client-side scripting language. Using JavaScript, we can develop interactive web sites and it is well influenced by many languages and it is very easy to learn and develop interactive web sites and web applications. XML: What is XML?
While I was working on a project, I have faced many browser compatibility issues like how to ignore white space while writing code in JavaScript. I observed that, IE (Internet Explorer) browser is ignoring white space but Mozilla (Firefox) and other Netscape Browsers are not ignoring white spaces. Where I have faced the issue? When I was trying to retrieve the length of childnodes in respective parent node. While I was retrieving the same Mozilla Firefox Browser behaves in a different way that IE browser. Actually, I have only ‘2 child nodes’ in a parent node. IE shows the length (nParentNode[0].childNodes.length) as 2 but when I execute the same code in mozilla the respective result was different - this was not my expected result, it should be 2 in mozilla as well. After doing some research on this, I observed that IE is ignoring whitespaces but coming to Mozilla and other browser’s were not ignoring whitespaces, to over come this issue we have to use one extra line of code in JavaScript: for (var j=0;j<nParentNode[0].childNodes.length;j++) If you look at the above code, the condition in ‘if statement’ does our job. My plan is to ignore whitespaces, this is possible using ‘nodeType‘ the above if condition checks for ELEMENT_NODE, if it is other than ELEMENT_NODE that is ATTRIBUTE_NODE (or) TEXT_NODE (or) ENTITY_NODE etc., it continues the loop. Below table depicts NodeTypes and its Named Constants:
Look at the XML file, which I am using as an example: <?xml version="1.0" encoding="utf-8"?> In the above XML, <Orgs> is parent tag to <Organization> and <Technology> that is <Organization> and <Technology> are child nodes. My aim here is to know how many child nodes are there in <Orgs> node, and to get data from those child tags. If you look at the JavaScript code:
nParentNode is pointing to <Orgs> and nParentNode[0] indicates the very first node of parent, childNodes is a property which returns a NodeList containing the child nodes of the respective selected node. length depicts the number of child nodes. nParentNode[0].childNodes.length If you execute the above line in IE browser it gives the result as “2″ but if you execute the same in Mozilla Firefox or other Netscape Browsers it gives the result as “4″ because of whitespace. So, in order to over come this types of issues we can make use of nodeType and our problem can be solved, that is a simple ‘if condition’ will solve our problem. XML Filename: data.xml <?xml version="1.0" encoding="utf-8"?> JavaScript JavaScript Filename (AJAX Code): data.js //Author: DeveloperSnippets.com HTML HTML Filename: data.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Hope this might help you in solving some of the issues which you are facing. If you have any comments, please do let me know. source: developersnippets |
|||||
| Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy |