简体   繁体   中英

Create dynamic link from RSS results in javascript

I'm using a Google Feed API to call an RSS feed into a conditional div on my site, and would like to return the URL of each feed item as a link, but I'm having no luck. The feed is returning and displaying fine based on the request, but I can't seem to figure out the createTextNode(entry.link).

Even a push in the right direction would be great. I'm willing to find the answer, but I'm just not searching correctly it seems...

Code:

google.load("feeds", "1");

// Callback function, for when a feed is loaded.
function feedLoaded(result) {
  if (!result.error) {
    // Grab the container we will put the results into
    var container = document.getElementById("announcements");
    container.innerHTML = '';

    // Loop through the feeds, putting the titles onto the page.
    // Check out the result object for a list of properties returned in each entry.
    // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
      var div = document.createElement("div");
      var entryStr = entry.title.indexOf('');

      // Formatting box for feed
      var span = document.createElement("span");
      span.className = "subhead";

      // Spacing elements
      var lineBreak = document.createElement("br");
      var paraBreak = document.createElement("p");

      // Conditional statement; if the returned entry contains '', run append statements
      if(entryStr != -1) {
        div.appendChild(document.createTextNode(entry.publishedDate));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.title));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.link));
        container.appendChild(span);
        span.appendChild(div);
      }
    }
  }
}

function OnLoad() {
  // Create a feed instance that will grab the appropriate feed.
  var feed = new google.feeds.Feed("http://*feedaddress*");
  feed.setNumEntries(4);
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);

Well I figured it out. I did the following (see // Link Creation, appended:

google.load("feeds", "1");

// Callback function, for when a feed is loaded.
function feedLoaded(result) {
  if (!result.error) {
    // Grab the container we will put the results into
    var container = document.getElementById("announcements");
    container.innerHTML = '';

    // Loop through the feeds, putting the titles onto the page.
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
      var div = document.createElement("div");
      var date = new Date(entry.publishedDate);
      var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
      var newDate =  months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
      var entryStr = entry.title.indexOf('');

      // Formatting box for feed
      var span = document.createElement("span");
      span.className = "subhead";

      // Spacing elements
      var lineBreak = document.createElement("br");
      var paraBreak = document.createElement("p");
      var parbBreak = document.createElement("p");

      // Link creation
      a = document.createElement('a');
      a.href = (entry.link);
      a.innerHTML = " Read More...";


      // Conditional statement; if the returned entry contains '', run append statements
      if(entryStr != -1) {
        div.appendChild(document.createTextNode(newDate));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.title));
        div.appendChild(a);
        container.appendChild(span);
        span.appendChild(div);
      }
    }
  }
}

function OnLoad() {
  // Create a feed instance that will grab the feed.
  var feed = new google.feeds.Feed("my site");
  feed.setNumEntries(5);
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);

The only issue now is getting the links to open in a new window. I suppose that's a new question, though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM