Search code examples
javascriptrss

Reading RSS with javascript


I am trying to read an RSS feed (here, the Google News feed) and get specific info out of the raw code using Javascript. The below code works when the URL in the second line is changed to a local file (I copy-pasted the gNews and saved as xml) in Firefox, but not when it is an external page as below. The code does not work under any conditions in IE or Chrome. Any suggestions?

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","https://news.google.com/news/feeds?ned=us&topic=h&output=rss",false);
try {
    xmlhttp.send();
}
catch(exception){
    document.write(exception+"");
}
var xmlDoc=xmlhttp.responseXML; 
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<4;i++) {
  if(i%2==0) {
    document.write("<div id='bottombox'>");
  }
  var raw=x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;<br />
  var toWrite=""+raw.substring(0,raw.indexOf(" - ",0));
  document.write("<a href="+x[i].getElementsByTagName('link')[0].childNodes[0].nodeValue+" style='font-family:Verdana, Geneva, sans-serif; font-size:13px; color:#FFF;'>");
  document.write(toWrite);
  document.write("</a>");
  document.write("<br />");
  if(i%2!=0) {
      document.write("</div>");
  }
  }

Solution

  • This is a cross-origin security violation, and prohibited by your browser.

    See "Same Origin Policy" and "Cross Origin Resource Sharing" for more information.