HomeTravel ReportLocal FoodRail&BusAirportMileagemiscAbout Me
TechWiki
Updated : January 2011

Google AJAX Feed API

RSSフィードの読み込み。

例)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<title>News</title>
<style type="text/css">
span.source {font: 14px "sans-serif"; margin:0px; color:black; word-break: break-all;}
span.pdate  {font: 10px "sans-serif"; margin:0px; color:green; word-break: break-all;}
span.title  {font: 14px "sans-serif"; margin:0px; color:black; word-break: break-all;}
span.content{font: 10px "sans-serif"; margin:0px; color:blue;  word-break: break-all;}
</style>
<script type="text/javascript" src="http://www.google.com/jsapi?key="(キー)"</script>
<script type = "text/javascript">
//<![CDATA[
google.load("feeds", "1");
google.setOnLoadCallback(getNews);
//
// ニュースRSS「朝日新聞」
//
function getNews(idx) {
 var url = 'http://rss.asahi.com/f/asahi_newsheadlines';
 
 var elmDiv = document.createElement("div");
 document.getElementsByTagName("body")[0].appendChild(elmDiv);
 elmDiv.innerHTML = 'Loading...';
 if (!idx)
  idx = defaultNews;
 var feed = new google.feeds.Feed(url);
 feed.setNumEntries(11);
 feed.load(function(result) {
  if (result.error) {
   var errmsg = 'Error:Google AJAX Feed API Code=' + result.error.code + '<br/>';
   errmsg += result.error.message + '<br/>';
   errmsg += 'URL="' + url + '"<br/>';
   elmDiv.innerHTML = errmsg;
  }
  else {
   var news_html = '<span class="source">【朝日新聞ニュース】</span><br/>';
   for (var i = 0; i < result.feed.entries.length; i++){
    // 'AD:' 広告以外を表示
    if (result.feed.entries[i].title.substr(0, 3) != 'AD:'){
     var pdate = new Date(result.feed.entries[i].publishedDate);
     news_html += '<span class="pdate">' + pdate.toLocaleString() + '</span><br/>';
     news_html += '<a href="' + result.feed.entries[i].link + '" target="_blank">';
     news_html += '<span class="title">' + result.feed.entries[i].title + '</span></a><br/>';
     news_html += '<span class="content">' + result.feed.entries[i].contentSnippet + '</span><br/>';
    }
   }
   elmDiv.innerHTML = news_html;
  }
 });
}
//]]>
</script>
</head>
<body>
</body>
</html>



Copyright(c) 2012-2013 T.F.T. All rights reserved.