del.icio.usでcallback
del.icio.usではJSONによるブラウザからの直接フィードインクルードが可能。
上記ページには、フィードをインクルードする場合は以下のように<script>タグをHTML中に記述する、と書いてある。
<script type="text/javascript" src="http://del.icio.us/feeds/json/stomita?count=10"></script>
<script>タグのsrc属性のURL
http://del.icio.us/feeds/json/stomita?count=10
(stomitaはdel.icio.us id)
から実際に返されるスクリプトの中身はこんなかんじ(改行整形済み)
if(typeof(Delicious) == 'undefined') Delicious = {};
Delicious.posts = [
{"u":"http://practical-scheme.net/trans/beating-the-averages-j.html",
"d":"Beating the Averages",
"t":["lisp"]},
{"u":"http://www.sarugau.org/",
"d":"Sarugau.org",
"t":["serverside","javascript","java"]},
....
]しかし実は、Yahoo!(US)のJSON形式のWebサービスと同じように、callbackパラメータを与えることによってJSONデータを任意のJavaScriptコールバック関数にハンドルさせることができる。
src属性のURLを以下のように変更。
http://del.icio.us/feeds/json/stomita?count=10&callback=handleFeeds
返されるデータは次のような感じ(改行整形済み)
handleFeeds([
{"u":"http://practical-scheme.net/trans/beating-the-averages-j.html",
"d":"Beating the Averages",
"t":["lisp"]},
{"u":"http://www.sarugau.org/",
"d":"Sarugau.org",
"t":["serverside","javascript","java"]},
....
])そのままJavaScriptの関数呼び出しになっているので、あとはもとのHTML中にコールバック関数 handleFeeds を適当に定義しておけばよい。