除了IE之外,Javascript不会在任何其他浏览器中运行。为什么?

时间:2013-03-31 18:20:21

标签: javascript

我遇到一个问题,让它在firefox,Android OS和其他浏览器中运行。谁知道为什么?我尝试了不同的解析,并查看了一堆文档,包括https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest

function callNews()
{
   var url = "someUrlHere";
   var xmlhttp = new XMLHttpRequest();
   var xmlDoc = null;

for (var i = 1; i < 20; i++)
{

    xmlhttp.open("GET", url, false);
    xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
    xmlDoc = xmlhttp.responseXML;

    var url2 = xmlDoc.getElementsByTagName("link")[i].textContent;
    var newsTitle = xmlDoc.getElementsByTagName("title")[i].textContent;
    var des = xmlDoc.getElementsByTagName("description")[i].textContent;
    //var newsDate = xmlDoc.getElementsByTagName("pubDate")[i].textContent;
    document.getElementById("desc" + i).innerHTML = newsTitle + "<br /><i style=\"color:red;\">" + des + "</i><br /><a href=\"" + url2 + "\">" + url2 + "</a>" + "<br /><br />" + "<iframe src=\"https://www.facebook.com/plugins/like.php?href=http://www.website.com\"scrolling=\"no\" frameborder=\"0\"style=\"border:none; width:450px; height:80px\"></iframe>";
    xmlhttp.send();
}

document.getElementById("desc" + 1).innerHTML = "Testing";
}

更新了代码......仍然无效

1 个答案:

答案 0 :(得分:1)

这对我来说效果更好

function callNews()
{
var url = "urlGoesHere";

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    alert("Old");
}
xmlhttp.open("GET", url, false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName("title");
var d = document.createElement("div");
var news = new Array();
var word = "";
for (var i = 0; i < x.length; i++)
{
    news[i] = "<a href=\"" + xmlDoc.getElementsByTagName("link")[i].childNodes[0].nodeValue + "\" style=\"text-decoration:none;\"  >" + xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue + "</a>";
    news[i] = "<b>" + news[i] + "</b><br /><em style=color:#555;> " + xmlDoc.getElementsByTagName("description")[i].childNodes[0].nodeValue + "</em>";
    word = word + " " + news[i] + "<br /><br />"
}
document.getElementById("newsUpdate0").innerHTML = word;
}