動かしてみる<!DOCTYPE html><html><head><title>JS</title><script>onload = function() {
alert(“ABC”);}</script></head><body></body></html>
タグを作る<!DOCTYPE html><html><head><title>JS</title><script>onload = function() {
var div = document.createElement("div");div.innerHTML = “鯖江”;document.body.appendChild(div);
}</script></head><body></body></html>
CSSとの組み合わせ<style>#t1 { color: #f00; }</style><script>onload = function() {
var div = document.createElement("div");div.innerHTML = "鯖江";div.id = "t1";document.body.appendChild(div);
}</script>
ボタン<script>add = function(s) {
var div = document.createElement("div");div.innerHTML = s;div.id = "t1";document.body.appendChild(div);
}</script><body><button onclick=‘add(“鯖江”)'>オス</button></body>
jsonp関数を作る
jsonp = function(url) {var script = document.createElement("script");script.setAttribute("src", url);var head =
document.getElementsByTagName("head")[0];head.appendChild(script);
}
Twitter検索関数twget = function() {
var tag = '鯖江';var url = "http://search.twitter.com/search.json";jsonp(url + "?callback=tw&q=" + encodeURI(tag));
}
※詳細な仕様はこちらhttp://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search
検索結果表示関数tw = function(data) {
for (var i = 0; i < data['results'].length; i++) {var twi = data['results'][i];add(twi['from_user'] + ": " + twi['text']);
}}
呼び出し部分<!DOCTYPE html><html><head><script>add = function(s) 略jsonp = function(url) 略twget = function() 略tw = function(data) 略</script><body><button onclick='twget()'>オス</button></body></html>