Javascript works great locally, but not on my server

Posted by Jonathan Cohen on Stack Overflow See other posts from Stack Overflow or by Jonathan Cohen
Published on 2010-12-30T22:31:41Z Indexed on 2010/12/30 22:54 UTC
Read the original article Hit count: 278

Filed under:
|
|

I'm teaching myself javascript by creating a script for displaying an external rss feed on a webpage.

The code I patched together works great locally. This is a screen grab of the code producing exactly the desired behavior. The code is populating all the information inside the section "Blog: Shades of Gray", except for "tagged" which I hard coded:

http://screencast.com/t/fNO5OPnmGPm2

But when I upload the site files to my server, the code doesn't work at all. This is a screen grab of the code on my site NOT producing the desired behavior...

alt text

This feels like I'm not getting something really basic about how javascript works locally vs. on the server. I did my half hour of googling for an answer and no trails look promising. So I'd really appreciate your help.

This is my site (under construction) http://jonathangcohen.com

Below is the code, which can also be found at http://jonathangcohen.com/grabFeeds.js.

/*Javascript for Displaying an External RSS Feed on a Webpage
Wrote some code that’ll grab attributes from an rss feed and assign IDs for displaying on a webpage. The code references my Tumblr blog but it’ll extend to any RSS feed.*/

window.onload = writeRSS;

function writeRSS(){
    writeBlog();
}

function writeBlog(){
    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://blog.jonathangcohen.com/rss.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    var x=xmlDoc.getElementsByTagName("item");
    //append category to link
    for (i=0;i<3;i++)
      {     
      if (i == 0){
          //print category
          var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
          document.getElementById("getBlogCategory1").innerHTML =  
         '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
          //print date
          var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
          thisDate = new Date();
          thisDate = formatTumblrDate(k);
          document.getElementById("getBlogPublishDate1").innerHTML = thisDate;
          //print title
          var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
          var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
          document.getElementById("getBlogTitle1").innerHTML =
          '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
          }
     if (i == 1){
        //print category
        var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
        document.getElementById("getBlogCategory2").innerHTML = 
        '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
        //print date
        var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
        thisDate = new Date();
        thisDate = formatTumblrDate(k);
        document.getElementById("getBlogPublishDate2").innerHTML = thisDate;
        //print title
        var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
        var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
        document.getElementById("getBlogTitle2").innerHTML =
        '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
        }
     if (i == 2){
        //print category
        var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
        document.getElementById("getBlogCategory3").innerHTML = 
        '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
        //print date
        var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
        thisDate = new Date();
        thisDate = formatTumblrDate(k);
        document.getElementById("getBlogPublishDate3").innerHTML = thisDate;
        //print title
        var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
        var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
        document.getElementById("getBlogTitle3").innerHTML =
        '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
        }
      }
}

function formatTumblrDate(k){
    d = new Date(k);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    printDate = (curr_month + "/" + curr_date + "/" + curr_year);
    return printDate;
}

Thank you!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about Xml