JSValidation Chrome适配修改
JSValidation是比较老的库了,Author: Michael Chen(mechiland) on 2004/03,最后一次更新还是在2004年,后面更多更方便的验证库出来之后,大概早就没人在用了吧,作者的博客也404了。没人看的话就当是自己记笔记了。
总之,机缘巧合,突然用到这个发现在chrome(53)下加载不到配置文件,在IE8下倒是可以,仔细看了下源码发现确实有问题,查了下chrome怎么加载xml文件,改了改就能用了。虽然可能别人用到这个古董的可能性不大,但是还是记录下吧!
原作者博客不能用,贴一个其他的介绍的地址:http://blog.csdn.net/kalision/article/details/7250939
另一个参考的博客地址:http://blog.csdn.net/nick848/article/details/11553985
原来的函数
function XmlDocument() {}
XmlDocument.create = function () {
if (document.implementation && document.implementation.createDocument) {
return document.implementation.createDocument("", "", null);
} else if (window.ActiveXObject) {
try {
var prefix = ["MSXML2", "MSXML", "Microsoft", "MSXML3"];
for (var i = 0; i < prefix.length; i++) {
//return new ActiveXObject(prefix[i] + ".DomDocument");
var obj = new ActiveXObject(prefix[i] + ".DomDocument");
if (obj == null || typeof(obj) == 'undefined') {
continue;
} else {
return obj;
}
}
} catch (e) {
//^_^
throw new Error("My God, What version of IE are you using? IE5&+ is requiered.");
}
} else
throw new Error("Cannot create DOM Document!");
}
修改后的函数
function XmlDocument() {}
XmlDocument.create = function (file) {
if (document.implementation && document.implementation.createDocument) {
//修改位置,方法是另一个作者的博客 http://blog.csdn.net/nick848/article/details/11553985
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", file, false);
xmlhttp.send(null);
return xmlhttp.responseXML;
} else if (window.ActiveXObject) {
try {
var prefix = ["MSXML2", "MSXML", "Microsoft", "MSXML3"];
for (var i = 0; i < prefix.length; i++) {
var obj = new ActiveXObject(prefix[i] + ".DomDocument");
if (obj == null || typeof(obj) == 'undefined') {
continue;
} else {
//修改位置,这里只是把原作者代码调整下
obj.async = false; // Damn!!! it cost me half an hour to fix it!
obj.load(file);
console.log(obj);
return obj;
}
}
} catch (e) {
//^_^
throw new Error("My God, What version of IE are you using? IE5&+ is requiered.");
}
} else
throw new Error("Cannot create DOM Document!");
}
在demo里测试了下,就可以正常使用了。