Web page effects load XML file multiple methods (compatible Ff,ie6+,opera)
Below we provide three kinds of JS loading XML documents, each load XML file method is different, but the principle is the same, the best is the third section, the instance is compatible with multiple browsers, the second instance of the, copy down can be directly used
Method One
var xmldoc;
Try{//ie
Create an empty Microsoft XML Document Object
Xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
}catch (e) {
Try{//ff
Create an empty XML Document object
Xmldoc=document.implementation.createdocument ("", "", null);
}catch (e) {
alert (e.message);
}
}
try{
Turns off asynchronous loading, which ensures that the parser does not continue to execute scripts until the document is fully loaded
Xmldoc.async=false;
Load XML document
Xmldoc.load (Xml_file);
}catch (e) {
alert (e.message);
return false;
}
return xmldoc;
Method Two, JS loads the XML and displays it
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<TITLE>JS loads XML and displays it </title>
<script type= "text/web Effects" >
var xmlhttp;
function GetData ()
{
//Create asynchronous Object
xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
//Load Server-Note No parameters
Xmlhttp.open ("Get", "XMLFile.xml", True)
//Asynchronous Object Event Hooks
Xmlhttp.onreadystatechange=statechange;
//Send request-no parameters
xmlhttp.send (NULL);
}
function StateChange ()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
//Get all returned data
var Data=xmlhttp.responsetext;
//Display results
document.getElementById ("Divlist"). Innerhtml=data;
}
}
</script>
</head>
<body>
<table style= "Text-align:center" >
<tr>
<TD style= "Text-align:center" >
displays data obtained from XML files </td>
</tr>
<tr>
<td style= "Text-align:center" >
<input id= "button1" type= "button" value= "Get Table" onclick= "GetData ()"/></td>
</tr>
<tr>
<td style= "Text-align:center" >
<div id= "Divlist" >
</div>
</td>
</tr>
</table>
</body>
</html>
Method Three supports Ff,ie6+,opera
Function getxmldocument (file) {
var xmldoc
try{//internet Explorer
xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
}
catch (e) {
try{/*firefox, Mozilla, Opera, etc.*/
xmldoc= Document.implementation.createdocument ("", "", null);
}
catch (e) {
alert (e.message);
return;
}
}
Xmldoc.async=false
xmldoc.load (URL);
return xmldoc;
}