just do it

解决javascript xml dom 之浏览器兼容

javascript xml dom 操作时,由于浏览器差异会有不同的问题。
ie浏览器下:new ActiveXObject(‘Microsoft.XMLDOM’);
Firefox, Mozilla, Opera, etc 下:document.implementation.createDocument(“”,””,null);
谷歌浏览器以及搜狗浏览器(document对象没有load方法)..下:
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open(“GET”,file,false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script language="javascript">
 
function loadXMLDoc(file){
 
	var xmlDoc;
 
	try //Internet Explorer
	{
	 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	 xmlDoc.async=false;
	 xmlDoc.load(file);
	}
	catch(e)
	{
	 try //Firefox, Mozilla, Opera, etc.
	 {
	  xmlDoc=document.implementation.createDocument("","",null);
	  xmlDoc.async=false;
	  xmlDoc.load(file);
	 }
	 catch(e)
	 {
	  try //Google Chrome  -Object Document has no method 'load' 
	  {
	   var xmlhttp = new window.XMLHttpRequest();
	   xmlhttp.open("GET",file,false);
	   xmlhttp.send(null);
	   xmlDoc = xmlhttp.responseXML.documentElement;
	  }
	  catch(e)
	  {
	   error=e.message;
	  }
	 }
	}
 
	return xmlDoc;
}
 
var xml=loadXMLDoc("xml.xml");
alert(xml);
 
 
</script>
点赞