Friday, October 21, 2016

Historical tech post: Bugs I have seen - Firefox and synchronous AJAX (01 Dec 2006)


 There is a problem with the way FireFox (in 2006) handles the Javascript XMLHttpRequest object. This object is used to implement AJAX techniques that allow access to remote information via HTTP without refreshing the whole web page. If the open method is called synchronously, the readyState property never gets set to 4 (completed). Some blogs suggest declaring the onreadystatechange function after the call to open, but this didn’t work for me (I had to use an asynchronous call). My page behaves better with the asynchronous call, but from what I’ve seen in the blogs, FireFox is either the only browser that does it right, or the only browser that does it wrong.
Also: I had a few issue when loading an XML document with Javascript. My XML document was being cached by the browser, and this caching apparently cannot be avoided with an HTML no-cache header. A simple but hack-ish solution is to do,
 xmlDok.load("file.xml?rnd=" + Math.random(1000).toString());
 so that the browser thinks a different URL is loaded each time.
 Also, one would expect to be able to select nodes with XPath, via selectSingleNode and selectNodes. This works as expected in MSIE, but Firefox implements a different DOM which throws exceptions ("selectNodes is not a function" or "selectSingleNode is not a function"). Try these:
 function XSelectNodes(xmldok, elementPath)
 {
 if (window.ActiveXObject)
 {
 return xmldok.selectNodes(elementPath);
 }
 else
 {
 var xpe = new XPathEvaluator();
 var nsResolver = xpe.createNSResolver((xmldok.ownerDocument == null) ? xmldok.documentElement : xmldok.ownerDocument.documentElement);
 var results = xpe.evaluate(elementPath, xmldok, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
 var i, nodes = [];
 for (i=0; i < results.snapshotLength; i++)
 {
 nodes[i] = results.snapshotItem(i);
 nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : "";
 }
 return nodes;
 }
 }
 function XSelectSingleNode(xmldok, elementPath)
 {
 if (window.ActiveXObject)
 {
 return xmldok.selectSingleNode(elementPath);
 }
 else
 {
 var xpe = new XPathEvaluator();
 var nsResolver = xpe.createNSResolver((xmldok.ownerDocument == null) ? xmldok.documentElement : xmldok.ownerDocument.documentElement);
 var results = xpe.evaluate(elementPath, xmldok, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
 return results.singleNodeValue;
 }
 }

No comments:

Post a Comment