<html> <head> <title>Test</title> <script language = "javascript" > function XmlDom() { //通过对象/属性检测法,判断是IE来是Mozilla if (window.ActiveXObject) { var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
for (var i=0; i < arrSignatures.length; i++) { try {
var oXmlDom = new ActiveXObject(arrSignatures[i]);
return oXmlDom;
} catch (oError) { //ignore } } throw new Error("MSXML is not installed on your system.");
//同上 } else if (document.implementation && document.implementation.createDocument) {
var oXmlDom = document.implementation.createDocument("","",null); //创建Mozilla版本的parseError对象 oXmlDom.parseError = { valueOf: function () { return this.errorCode; }, toString: function () { return this.errorCode.toString() } };
//初始化parseError对象 oXmlDom.__initError__();
oXmlDom.addEventListener("load", function () { this.__checkForErrors__(); this.__changeReadyState__(4); }, false); return oXmlDom;
} else { throw new Error("Your browser doesn't support an XML DOM object."); } }//此处用到了该书中一个浏览器系统检测js文件,如果是Mozilla if (document.addEventListener) { Document.prototype.readyState = 0; Document.prototype.onreadystatechange = null; Document.prototype.__changeReadyState__ = function (iReadyState) { this.readyState = iReadyState; if (typeof this.onreadystatechange == "function") { this.onreadystatechange(); } }; //初始化parseError对象 Document.prototype.__initError__ = function () { this.parseError.errorCode = 0; this.parseError.filepos = -1; this.parseError.line = -1; this.parseError.linepos = -1; this.parseError.reason = null; this.parseError.srcText = null; this.parseError.url = null; };
Document.prototype.__checkForErrors__ = function () { if (this.documentElement.tagName == "parsererror") { var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/; reError.test(this.xml);
//定义Mozilla的loadXML方法 Document.prototype.loadXML = function (sXml) {
this.__initError__();
this.__changeReadyState__(1);
var oParser = new DOMParser(); var oXmlDom = oParser.parseFromString(sXml, "text/xml");
while (this.firstChild) { this.removeChild(this.firstChild); } for (var i=0; i < oXmlDom.childNodes.length; i++) { var oNewNode = this.importNode(oXmlDom.childNodes[i], true); this.appendChild(oNewNode); }
load()方法重写了,按照道理来说应当执行:this.__initError__();才对,但是没有执行啊?我又在
this.__initError__();之前加一句alert("test");看一下能不能输出,测试的结果还是不能输出.
如下: Document.prototype.__load__ = Document.prototype.load;//更新Mozilla的load信息
Document.prototype.load = function(sRUL)//更新Mozilla的load信息
{
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sRUL);
}
Node.prototype.__defineGetter__("xml",function()//为每个节点添加xml特性,以便和IE实现一样
{
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(this,"text/xml");
});
}
<head>
<title>Test</title>
<script language = "javascript" >
function XmlDom() {
//通过对象/属性检测法,判断是IE来是Mozilla
if (window.ActiveXObject) {
var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
"MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument",
"Microsoft.XmlDom"];
for (var i=0; i < arrSignatures.length; i++) {
try {
var oXmlDom = new ActiveXObject(arrSignatures[i]);
return oXmlDom;
} catch (oError) {
//ignore
}
} throw new Error("MSXML is not installed on your system.");
//同上
} else if (document.implementation && document.implementation.createDocument) {
var oXmlDom = document.implementation.createDocument("","",null); //创建Mozilla版本的parseError对象
oXmlDom.parseError = {
valueOf: function () { return this.errorCode; },
toString: function () { return this.errorCode.toString() }
};
//初始化parseError对象
oXmlDom.__initError__();
oXmlDom.addEventListener("load", function () {
this.__checkForErrors__();
this.__changeReadyState__(4);
}, false); return oXmlDom;
} else {
throw new Error("Your browser doesn't support an XML DOM object.");
}
}//此处用到了该书中一个浏览器系统检测js文件,如果是Mozilla
if (document.addEventListener) { Document.prototype.readyState = 0;
Document.prototype.onreadystatechange = null;
Document.prototype.__changeReadyState__ = function (iReadyState) {
this.readyState = iReadyState; if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
};
//初始化parseError对象
Document.prototype.__initError__ = function () {
this.parseError.errorCode = 0;
this.parseError.filepos = -1;
this.parseError.line = -1;
this.parseError.linepos = -1;
this.parseError.reason = null;
this.parseError.srcText = null;
this.parseError.url = null;
};
Document.prototype.__checkForErrors__ = function () { if (this.documentElement.tagName == "parsererror") { var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/; reError.test(this.xml);
this.parseError.errorCode = -999999;
this.parseError.reason = RegExp.$1;
this.parseError.url = RegExp.$2;
this.parseError.line = parseInt(RegExp.$3);
this.parseError.linepos = parseInt(RegExp.$4);
this.parseError.srcText = RegExp.$5;
}
};
//定义Mozilla的loadXML方法
Document.prototype.loadXML = function (sXml) {
this.__initError__();
this.__changeReadyState__(1);
var oParser = new DOMParser();
var oXmlDom = oParser.parseFromString(sXml, "text/xml");
while (this.firstChild) {
this.removeChild(this.firstChild);
} for (var i=0; i < oXmlDom.childNodes.length; i++) {
var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
this.appendChild(oNewNode);
}
//载入后检查错误
this.__checkForErrors__();
//没有问题,设置readyState属性为4
this.__changeReadyState__(4); };
Document.prototype.__load__ = Document.prototype.load; Document.prototype.load = function (sURL) {
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sURL);
};
Node.prototype.__defineGetter__("xml", function () {
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(this, "text/xml");
});} var oXmlDom = new XmlDom();
oXmlDom.onreadystatechange = function()
{
if(oXmlDom.readyState == 4)
{
if(oXmlDom.parseError != 0)
{
var oError = oXmlDom.parseError;
alert("An error occurred:\nError Code:" + oError.errorCode + "\n" + "Line: " + oError.line + "\n" + "reason: " + oError.reason);
}
else
{
alert(oXmlDom.xml + "\n为什么啊?");
}
}
}
oXmlDom.load("test.xml");
</script>
</head>
<body>
</body>
</html>
不过我测试了此代码,在__initError__加了一个alert(0)
被调用一次.你的一次没调用,是不是你没刷新缓存啊
这里
if(document.implementation && document.implementation.createDocument)//初始化Mozilla信息
{
//后加的
if (typeof XMLDocument != "undefined") {
Document = XMLDocument;
} else if (typeof Document != "undefined") {
Document = Document;
}
...
<head>
<title>Test</title>
<script language = "javascript" >
function XmlDom()
{
if(window.ActiveXObject)
{
var arrSignatures = ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XmlDom"];
for(var i=0; i<arrSignatures.length; i++)
{
try
{
var oXmlDom = new ActiveXObject(arrSignatures[i]);//创建IE中的oXmlDom实例
return oXmlDom;
}
catch(exception)
{
//ignore
}
}
throw new Error("Your browser doesn't support an XML DOM object!!!!!");
}
else if(document.implementation && document.implementation.createDocument)
{
var oXmlDom = document.implementation.createDocument("","",null);//创建Mozilla中的oXmlDom实例
oXmlDom.parseError =
{
valueOf:function(){return this.errorCode},
toString:function(){return this.errorCode.toString()}
};
oXmlDom.__initError__();
oXmlDom.addEventListener("load",function()
{
this.__checkForErrors__();
this.__changeReadyState__(4);
},false);
return oXmlDom;
}
else
{
throw new Error("Your browser doesn't support an XML DOM object!!!!!");
}
}
if(document.implementation && document.implementation.createDocument)//初始化Mozilla信息
{
Document.prototype.readState = 0;//创建加载状态特性
Document.prototype.onreadystatechange = null;//创建加载事件
Document.prototype.__changeReadyState__ = function(iReadyState)//加载事件函数
{
this.readyState = iReadyState;
if(typeof this.onreadystatechange == "function")
{
this.onreadystatechange();
}
}
Document.prototype.__initError__ = function()//初始化Mozilla的错误信息
{
this.parseError.errorCode = 0;
this.parseError.filepos = -1;
this.parseError.line = -1;
this.parseError.linepos =-1;
this.parseError.reason = null;
this.parseError.srcText = null;
this.parseError.url = null;
}
Document.prototype.__checkForErrors__ = function()//检查错误信息
{
if(this.documentElement.tagName == "parsererror")
{
var reError = />[\s\S]*?>([\s\S]*?)位置[\s\S]*?([\s\S]*?)行[\s\S]*?(\d+)[\s\S]*?列[\s\S]*?(\d+)[\s\S]*?<sourcetext>([\s\S]*?)<\/sourcetext>/;
reError.test(this.xml);
this.parseError.errorCode = -999999;
this.parseError.reason = RegExp.$1;
this.parseError.url = RegExp.$2;
this.parseError.line = parseInt(RegExp.$3);
this.parseError.linepos = parseInt(RegExp.$4);
this.parseError.srcText = RegExp.$5;
}
}
Document.prototype.loadXML = function(sXml)//添加loadXML方法,以便和IE实现一样
{
this.__initError__();
this.__changeReadyState__(1);
var oParser = new DOMParser();
var oXmlDom = oParser.parseFromString(sXml,"text/xml");
while(this.firstChild)
{
this.removeChild(this.firstChild);
}
for(var i=0; i<oXmlDom.childNodes.length; i++)
{
var oNewNode = this.importNode(oXmlDom.childNodes[i],true);
this.appendChild(oNewNode);
}
this.__checkForErrors__();
this.__changeReadyState__(4);
}
Document.prototype.__load__ = Document.prototype.load;//更新Mozilla的load信息
Document.prototype.load = function(sRUL)//更新Mozilla的load信息
{
alert(1);
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sRUL);
}
Node.prototype.__defineGetter__("xml",function()//为每个节点添加xml特性,以便和IE实现一样
{
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(this,"text/xml");
});
}
var oXmlDom = new XmlDom();
oXmlDom.onreadystatechange = function()
{
if(oXmlDom.readyState == 4)
{
if(oXmlDom.parseError != 0)
{
var oError = oXmlDom.parseError;
alert("An error occurred:\nError Code:" + oError.errorCode + "\n" + "Line: " + oError.line + "\n" + "reason: " + oError.reason);
}
}
}
oXmlDom.load("test.xml");
</script>
</head>
<body>
</body>
</html>
alert(sURL);
alert(oXmlDom.load);
将输出
function load(){
[native code]
}
Document.prototype.__load__ = Document.prototype.load;//更新Mozilla的load信息
Document.prototype.load = function(sRUL)//更新Mozilla的load信息
{
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sRUL);
}
你怎么说我没有定义load方法啊?我还是不太明白你的意思,请指教.
if(document.implementation && document.implementation.createDocument)//初始化Mozilla信息
{
//在这里加上下面这段话就行了
if (typeof XMLDocument != "undefined") {
Document = XMLDocument;
} else if (typeof Document != "undefined") {
Document = Document;
}...
Sorry,写错了,是你没有重写load方法
IE哪有DOM HTML API ?
Document.prototype.__load__ = Document.prototype.load;//更新
Document.prototype.load = function(sRUL)//更新Mozilla的load信息
{
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sRUL);
}
这段话就没有作用了,如果前面没有用如下的代码:
//在这里加上下面这段话就行了
if (typeof XMLDocument != "undefined") {
Document = XMLDocument;
} else if (typeof Document != "undefined") {
Document = Document;
}