safari / IE错误x [i] .getElementsByTagName(" image")[1]中的错误未定义 [英] Error in safari/IE Error x[i].getElementsByTagName("image")[1] is undefined
问题描述
我试图在HTML中显示RSS源。它适用于iPad,iPhone和Chrome,但不适用于Internet Explorer或Safari。我收到一条错误消息,称
$ b $ $ p $ x [i] .getElementsByTagName(image)[1]未定义
有谁知道我如何才能使这个工作?它使用Apple RSS源来显示App Store中的应用程序。错误发生在第一个document.write。我试图在表中显示返回的结果。
< html>
< head>
< title> RSS应用程式< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< / head>
< body bgcolor =#FFFFFFleftmargin =0topmargin =0marginwidth =0marginheight =0>
< script type =text / javascript>
var xmlhttp;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.open(GET,http://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A% 2F%2Fwww.s2d6.com%2FX%2F%3FX%的3Dc%26Z%3DS%26V%3D3868801%26吨%3D,假);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName(feed);
for(i = 0; i< x.length; i ++)
{
document.write(< a href =+ x [i] .getElementsByTagName id)[1] .childNodes [0] .nodeValue +>< img src =+ x [i] .getElementsByTagName(im:image)[1] .childNodes [0] .nodeValue +> ;< / A>中);
document.write(< / td>< td>);
document.write(< a href =+ x [i] .getElementsByTagName(id)[2] .childNodes [0] .nodeValue +>< img src =+ x [i ] .getElementsByTagName(im:image)[4] .childNodes [0] .nodeValue +>< / a>);
document.write(< / td>< td>);
document.write(< a href =+ x [i] .getElementsByTagName(id)[3] .childNodes [0] .nodeValue +>< img src =+ x [i ] .getElementsByTagName(im:image)[7] .childNodes [0] .nodeValue +>< / a>);
document.write(< / td>< / tr>< tr>< td>);
document.write(< font size = \1 \>< a href =+ x [i] .getElementsByTagName(id)[1] .childNodes [0] .nodeValue +>+ x [i] .getElementsByTagName(name)[1] .childNodes [0] .nodeValue +< / a>);
document.write(< / td>< td>);
document.write(< font size = \1 \>< a href =+ x [i] .getElementsByTagName(id)[1] .childNodes [0]。 nodeValue +>+ x [i] .getElementsByTagName(name)[2] .childNodes [0] .nodeValue +< / a>);
document.write(< / td>< td>);
document.write(< font size = \1 \>< a href =+ x [i] .getElementsByTagName(id)[1] .childNodes [0]。 nodeValue +>+ x [i] .getElementsByTagName(name)[3] .childNodes [0] .nodeValue +< / a>);
}
< / script>
< / td>< / tr>
< / table>
< / div>
< / body>
< / html>
更新了代码以处理im:image,代码现在可以在Internet Explorer和Safari浏览器中使用,但不能in chrome。
这会给出结果。 DEMO
通知我访问 entry
元素,并在条目中访问im:image标签
更新在IE8中,Fx Safari和Chrome上的XP
< html>
< head>
< title> RSS应用程式< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< script type =text / javascript>
var isIE = navigator.userAgent.indexOf('MSIE')!= - 1;
var ns = {im:http://itunes.apple.com/rss}; //可以从根元素的属性中提取
$ b $ function getElems(obj,tagName){
if(!obj.getElementsByTagNameNS)return obj.getElementsByTagName(tagName);
var prefix =;
if(tagName.indexOf(:)!= - 1){
var parts = tagName.split(:)
prefix = parts [0];
tagName = parts [1];
}
if(prefix ==)return obj.getElementsByTagName(tagName);
return obj.getElementsByTagNameNS(ns [prefix],tagName);
}
window.onload = function(){
var xmlhttp;
if(window.XMLHttpRequest){//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else {//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.open(GET,http://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A% 2F%2Fwww.s2d6.com%2FX%2F%3FX%的3Dc%26Z%3DS%26V%3D3868801%26吨%3D,假);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName(entry);
var html =;
for(var i = 0; i< x.length; i ++){
var entry = x [i];
var id = getElems(entry,id)[0] .textContent;
var nameTag = getElems(entry,im:name);
var name = isIE?nameTag [0] .text:nameTag [0] .textContent;
html + ='< a href ='+ id +'>'+ name +'< br />';
var images = getElems(entry,'im:image'); $ j $ b $(j = 0; j html + ='< img src =''+(isIE?images [j] .text:images [j] .textContent)+ '/>';
}
html + ='< / a>< hr />';
}
document.getElementById('content')。innerHTML = html;
}
< / script>
< / head>
< body bgcolor =#FFFFFFleftmargin =0topmargin =0marginwidth =0marginheight =0>
< div id =content>< / div>
< / div>
< / body>
< / html>
更新2
jQuery版本 DEMO
<$ p对于IE(8),支持
$ .ajax({}(document).ready(function(){
jQuery.support.cors = true; // IMPERATIVE
类型:GET,
url:http://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A%2F %2Fwww.s2d6.com%2Fx%2F%3Fx%3Dc%26z%3Ds%26v%3D3868801%26t%3D,
dataType:xml,
success:function(xml){$ (); $ b $(xml).find('entry')。 = $(this).find(title)。text();
$(#content)。append('< hr />'+ title +'< br />') ;
var images = $(this).find(image);
if(images.length == 0)images = $(this).find(im\\:image );
$ .each(images,function(){
$(#co (< / a>');
});
});
}
});
});
I am trying to display an RSS feed in HTML. It works on iPad, iPhone and chrome but not in internet explorer or safari. I get an error message saying
x[i].getElementsByTagName("image")[1] is undefined
Does anyone know how I can get this to work? It is using an Apple RSS feed to display apps from the App Store. The error is occurring at the first document.write. I am trying to display the returned results in a table also.
<html>
<head>
<title>RSS Apps</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<script type="text/javascript">
var xmlhttp;
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://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A%2F%2Fwww.s2d6.com%2Fx%2F%3Fx%3Dc%26z%3Ds%26v%3D3868801%26t%3D",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("feed");
for (i=0;i<x.length;i++)
{
document.write("<a href="+x[i].getElementsByTagName("id")[1].childNodes[0].nodeValue+"><img src=" + x[i].getElementsByTagName("im:image")[1].childNodes[0].nodeValue +"></a>");
document.write("</td><td>");
document.write("<a href="+x[i].getElementsByTagName("id")[2].childNodes[0].nodeValue+"><img src=" + x[i].getElementsByTagName("im:image")[4].childNodes[0].nodeValue +"></a>");
document.write("</td><td>");
document.write("<a href="+x[i].getElementsByTagName("id")[3].childNodes[0].nodeValue+"><img src=" + x[i].getElementsByTagName("im:image")[7].childNodes[0].nodeValue +"></a>");
document.write("</td></tr><tr><td>");
document.write("<font size=\"1\"><a href="+x[i].getElementsByTagName("id")[1].childNodes[0].nodeValue+">" + x[i].getElementsByTagName("name")[1].childNodes[0].nodeValue+"</a>");
document.write("</td><td>");
document.write("<font size=\"1\"><a href="+x[i].getElementsByTagName("id")[1].childNodes[0].nodeValue+">" + x[i].getElementsByTagName("name")[2].childNodes[0].nodeValue+"</a>");
document.write("</td><td>");
document.write("<font size=\"1\"><a href="+x[i].getElementsByTagName("id")[1].childNodes[0].nodeValue+">" + x[i].getElementsByTagName("name")[3].childNodes[0].nodeValue+"</a>");
}
</script>
</td></tr>
</table>
</div>
</body>
</html>
Updated code to handle "im:image" and the code now works in internet explorer and safari but not in chrome.
This will give a result. DEMO
Notice I access the entry
elements and in the entries I access the im:image tags
UPDATE This works in IE8, Fx Safari and Chrome on XP
<html>
<head>
<title>RSS Apps</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var isIE = navigator.userAgent.indexOf('MSIE')!=-1;
var ns = {im:"http://itunes.apple.com/rss"}; // could be extracted from the root element's attributes
function getElems(obj,tagName) {
if (!obj.getElementsByTagNameNS) return obj.getElementsByTagName(tagName);
var prefix = "";
if (tagName.indexOf(":") !=-1) {
var parts = tagName.split(":")
prefix = parts[0];
tagName = parts[1];
}
if (prefix == "") return obj.getElementsByTagName(tagName);
return obj.getElementsByTagNameNS(ns[prefix], tagName);
}
window.onload=function() {
var xmlhttp;
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://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A%2F%2Fwww.s2d6.com%2Fx%2F%3Fx%3Dc%26z%3Ds%26v%3D3868801%26t%3D",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
var html = "";
for (var i=0;i<x.length;i++) {
var entry = x[i];
var id =getElems(entry,"id")[0].textContent;
var nameTag = getElems(entry,"im:name");
var name = isIE?nameTag[0].text:nameTag[0].textContent;
html += '<a href="'+id+'">'+name+'<br/>';
var images = getElems(entry,'im:image');
for (var j=0;j<images.length;j++) {
html += '<img src="'+(isIE?images[j].text:images[j].textContent)+'"/>';
}
html+='</a><hr/>';
}
document.getElementById('content').innerHTML=html;
}
</script>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div id="content"></div>
</div>
</body>
</html>
UPDATE 2
jQuery version DEMO
$(document).ready(function() {
jQuery.support.cors = true; // IMPERATIVE for IE(8) support
$.ajax({
type: "GET",
url: "http://itunes.apple.com/au/rss/topfreeapplications/limit=10/xml?partnerId=1002&partnerUrl=http%3A%2F%2Fwww.s2d6.com%2Fx%2F%3Fx%3Dc%26z%3Ds%26v%3D3868801%26t%3D",
dataType: "xml",
success: function(xml) {
$(xml).find('entry').each(function(){
var id = $(this).find("id").text();
var title = $(this).find("title").text();
$("#content").append('<hr/>'+title+'<br/>');
var images = $(this).find("image");
if (images.length ==0) images = $(this).find("im\\:image");
$.each(images,function(){
$("#content").append('<a href="'+id+'"><img src="'+$(this).text()+'"/></a>');
});
});
}
});
});
这篇关于safari / IE错误x [i] .getElementsByTagName(" image")[1]中的错误未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!