用于检测浏览器版本的XML/Javascript
我创建了一个XML文件,并试图使用web浏览器访问它。 当我使用IE时,脚本运行得非常好,一切正常,但当我尝试在其他浏览器中打开它时,它不起作用。 经过研究,我了解到我在JavaScript的第一行中引用的ActiveX内容与Microsoft相连。用于检测浏览器版本的XML/Javascript,javascript,html,xml,cross-browser,Javascript,Html,Xml,Cross Browser,我创建了一个XML文件,并试图使用web浏览器访问它。 当我使用IE时,脚本运行得非常好,一切正常,但当我尝试在其他浏览器中打开它时,它不起作用。 经过研究,我了解到我在JavaScript的第一行中引用的ActiveX内容与Microsoft相连。 这就是为什么我发现了另一个可以自动检查浏览器性质的代码: <script type="text/javascript"> if (window.XMLHttpRequest) { // code for IE7+, Firefo
这就是为什么我发现了另一个可以自动检查浏览器性质的代码:
<script type="text/javascript">
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "emp.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
//function loadXML(xmlFile)
//{
//xmlDoc.async="false";
//xmlDoc.onreadystatechange=verify;
//xmlDoc.load(xmlFile);
//xmlObj=xmlDoc.documentElement;
//}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“emp.xml”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
//函数loadXML(xmlFile)
//{
//xmlDoc.async=“false”;
//xmlDoc.onreadystatechange=verify;
//加载(xmlFile);
//xmlObj=xmlDoc.documentElement;
//}
实际上,函数的最后两行就足以加载XML文件。前两行代码的编写是为了确保我们稍后用来操作XML文件数据的JavaScript函数不会在未初始化的对象上执行任何函数。因此调用函数*verify()
<?php
//function verify()/
//{
// 0 Object is not initialized
// 1 Loading object is loading data
// 2 Loaded object has loaded data
// 3 Data from object can be worked with
// 4 Object completely initialized
//if (xmlDoc.readyState != 4)
// {
// return false;
//}
//}
?>
loadXML('emp.xml');
alert(xmlDoc.childNodes(0).firstChild.text);
alert(xmlDoc.childNodes(3).childNodes(1).firstChild.text);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
loadXML('emp.xml');
警报(xmlDoc.childNodes(0.firstChild.text);
警报(xmlDoc.childNodes(3.childNodes(1.firstChild.text));
我获取JScript运行时错误访问被拒绝。。。怎么办
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
我的新代码:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”http://www.multimediaprof.com/test/emp.xml“,对);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
警报(xmlDoc.childNodes[1].firstChild.text);
JQUERY
是答案吗,我该如何实现它?问题是您试图通过文件://
协议访问计算机上的文件。这是一种安全风险,大多数浏览器不允许这样做
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
解决此问题的方法是将文件托管在本地或在线服务器上
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
如果您使用的是Google Chrome,您可以使用--允许从文件访问文件
标志运行它,使其正常工作
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
编辑:我认为您在这里有一个语法错误:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
alert(xmlDoc.childNodes(0).firstChild.text);
childNodes
对象是数组,而不是函数。因此,应该使用方括号,[]
,而不是括号,()
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
EDIT2:如果使用jQuery,语法如下:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
$.get("file.xml", function(data){
//Your data is accessible through the data variable here!
console.log("Data Loaded: " + data);
});
更多文档:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
if(window.XMLHttpRequest){
xhttp=newXMLHttpRequest();
}else//适用于旧版IE 5/6
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”,“/Users/kart2006/Desktop/emp.xml”,false);
xhttp.send(“”);
xmlDoc=xhttp.responseXML;
document.write(xmlDoc.documentElement.nodeName+“已加载”);
var str=xmlDoc.getElementsByTagName(“to”)[0]。子节点[0]。节点值;
警报(str);
XML文件:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Edited by XMLSpy® -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
托弗
贾尼
提醒
这个周末别忘了我!
记住,我已经在Mac中加载了“emp.xml”文件。在windows的“文件://”中
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>
如果我没有弄错的话,我想你已经从不同的链接中获取了内容。请阅读这些链接:你给我的第二个链接是我开始使用的链接,它在IE以外的任何浏览器上都不起作用,这就是为什么我尝试将两个不同的链接放在一起,以尝试在任何浏览器上使用它,即使我尝试了包含测试功能的链接,发生JS运行时错误,它在任何浏览器上都无法运行…有什么想法吗?这里有一个非常简单的示例。记住,如何在本地加载文件。请验证文件“xmlDoc=loadXMLDoc(“books.xml”);”的路径你可以给出这样的“File://C:/text.xml”试试这个我试着把xml的位置放到我的服务器上,错误消失了!但是另一个出现了!您的解决方案使访问错误消失,但有一个新的错误,新的错误是他找不到childenodes,请注意,在将浏览器测试功能置于IE上之前,我能够在警报中看到结果,任何想法plzMy不好,它工作了。。你能分享更多关于访问本地xml文件的知识吗?我是itNB的新手,它在IE之外的任何浏览器上都不起作用。怎么办?在任何其他浏览器中,文档都被视为空。我确实修复了我遇到的语法错误,这些错误的概念是相同的:它被认为是空的objectxmlDoc在任何其他浏览器中都被认为是空的。。。怎么办?你的代码在IE上运行得很好,但在chrome和firefox上都无法运行。。。怎么办???@ELias Rahme:我已经在Firefox和Safari浏览器中成功测试了。对于google chrome,请检查这里:在Firefox中,它仍然会告诉我xml文档为空。。在Safari中,它只会在加载过程中被卡住,而没有发生任何事情。……我能做的就是让所有浏览器的XML加载不仅仅是IE吗?如果它是“NULL”,那么加载XML文件就有问题了。请检查文件的路径位置。请查看FF&Safari的屏幕截图。
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.multimediaprof.com/test/emp.xml",true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
alert(xmlDoc.childNodes[1].firstChild.text);
</script>
</head>
</html>