主题:  在 Firefox 中用 javascript 调用 XML

初级潜水员

职务:普通成员
等级:2
金币:1.0
发贴:431
#12005/10/24 22:25:46
这段代码在 IE 下能够工作,但在 Firefox 中不能运行。
我找到的空间不支持服务器技术。
哪位能指点一下,最符合 W3C 标准的代码应该怎么写?

谢谢

代码如下:
引用:
<script language="javascript">
<!--
var orderDoc = new ActiveXObject("MSXML2.DOMDocument" ) ;
orderDoc.load("order.xml" ) ;
var items = orderDoc.selectNodes("/Order/Item" ) ;

function display() {
//Display Somthing
}
-->
</script>



不是高手

职务:普通成员
等级:3
金币:5.0
发贴:1352
#22005/10/26 15:13:38
FF不支持ActiveXObject的,ActiveXObject是IE独有地!所以运行失败,这个属于先天问题吧,W3C标准就是这样啊,咔咔~~~



初级潜水员

职务:普通成员
等级:2
金币:1.0
发贴:431
#32005/10/28 2:52:48
那还有别的什么办法能在 FF 下 通过 JS 在客户端调用 XML 呢?

不行的话我两个通宵写的代码要全部推倒重来了,哭死……



不是高手

职务:普通成员
等级:3
金币:5.0
发贴:1352
#42005/10/28 17:33:06
好像不行,改在服务器端做吧



我佛山人

职务:版主
等级:4
金币:16.0
发贴:2269
#52005/11/2 14:02:26
var XmlReader = function(){
	this.GetHttpRequest = function(){
		if (window.XMLHttpRequest)		// Gecko
			return new XMLHttpRequest();
		else if (window.ActiveXObject)	// IE
			return new ActiveXObject("MsXml2.XmlHttp") ;
	}
	this.LoadUrl = function(urlToCall, asyncFunctionPointer){
		var oXmlReader = this;
		var bAsync = (typeof(asyncFunctionPointer) == "function");

		var oXmlHttp = this.GetHttpRequest();	
		oXmlHttp.open("GET", urlToCall, bAsync);
		if (bAsync){	
			oXmlHttp.onreadystatechange = function() 
			{
				if (oXmlHttp.readyState == 4){
					oXmlReader.DOMDocument = oXmlHttp.responseXML;
					if (oXmlHttp.status == 200)
						asyncFunctionPointer( oXmlReader ) ;
					else
						alert("XML request error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")" ) ;
				}
			}
		}
		
		oXmlHttp.send(null) ;
		
		if (!bAsync)
		{
			if (oXmlHttp.status == 200)
				this.DOMDocument = oXmlHttp.responseXML ;
			else
			{
				alert("XML request error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")") ;
			}
		}
	}
	this.SelectNodes = function(xpath){
		if (document.all)		// IE
			return this.DOMDocument.selectNodes(xpath) ;
		else{					// Gecko
			var aNodeArray = new Array();

			var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
			if (xPathResult){
				var oNode = xPathResult.iterateNext() ;
				while(oNode){
					aNodeArray[aNodeArray.length] = oNode ;
					oNode = xPathResult.iterateNext();
				}
			} 
			return aNodeArray ;
		}
	}
	this.SelectSingleNode = function(xpath){
		if (document.all)		// IE
			return this.DOMDocument.selectSingleNode( xpath ) ;
		else{					// Gecko
			var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument,
					this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null);

			if (xPathResult && xPathResult.singleNodeValue)
				return xPathResult.singleNodeValue ;
			else	
				return null ;
		}
	}
}



浮尘

职务:普通成员
等级:3
金币:7.0
发贴:1258
#62005/11/2 17:34:52
版主就是版主



不是高手

职务:普通成员
等级:3
金币:5.0
发贴:1352
#72005/11/3 15:59:47
太好了,我都不知道FF可以支持XMLHttpRequest()的