javascript异步读取xml各浏览器兼容

function readXml(path , handler) {
	var dom ;
	var ajax = false;
	if(window.ActiveXObject) {
		dom = new ActiveXObject("Microsoft.XMLDOM");
	} else  {
		var useragent = navigator.userAgent.toLowerCase();
		if(/mozilla/.test(useragent) && !/(?:compatible|webkit)/.test(useragent)) {
			dom = document.implementation.createDocument('' , '' , null);
		} else {
			dom = new XMLHttpRequest();
			ajax = true;
		}
	}
	if(ajax) {
		dom.onreadystatechange = function() {
			if(dom.readyState == 4 && dom.status == 200) {
				handler(dom.responseXML.documentElement);
			}
		};
		dom.open('GET' , path , true);
		dom.send(null);
	} else {
		dom.async = true;
		if(window.ActiveXObject) {
			dom.onreadystatechange = function() {
				if(dom.readyState == 4) {
					handler(dom.documentElement);
				}
			};
		} else {
			dom.onload = function() {
				handler(dom.documentElement);
			};
		}
		dom.load(path);
	}
};

 说明:本身读取xml的api在opera,safari,chrome上不支持异步读取,对于这三种浏览器采用的是ajax的get方式,ajax支持异步读取。而ie和firefox使用本身读取xml的api,本身就支持异步读取

你可能感兴趣的:(JavaScript,xml,Ajax,浏览器,chrome)