<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EC studio 技術ブログ</title>
	<atom:link href="http://techblog.ecstudio.jp/feed" rel="self" type="application/rss+xml" />
	<link>http://techblog.ecstudio.jp</link>
	<description>株式会社EC studioの技術ブログ</description>
	<lastBuildDate>Tue, 24 Apr 2012 01:34:56 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>チャットワーク開発の裏側</title>
		<link>http://techblog.ecstudio.jp/tech/backstage_of_chatwork.html</link>
		<comments>http://techblog.ecstudio.jp/tech/backstage_of_chatwork.html#comments</comments>
		<pubDate>Mon, 23 Apr 2012 11:34:16 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[EC studio 技術部]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=1414</guid>
		<description><![CDATA[大変ご無沙汰な技術ブログ更新となってしまいました。 振り返ってみると、前回の記事がもう約2年前！ ブログ記事を楽しみにしていただいていた方には申し訳ない限りです。 この2年間、何をやってたかというと、 「チャットワーク」 [...]]]></description>
			<content:encoded><![CDATA[<p>大変ご無沙汰な技術ブログ更新となってしまいました。<br />
振り返ってみると、前回の記事がもう約2年前！<br />
ブログ記事を楽しみにしていただいていた方には申し訳ない限りです。</p>
<p>この2年間、何をやってたかというと、<br />
「<a href="http://www.chatwork.com/ja/" target="_blank">チャットワーク</a>」というサービスの開発に全社を挙げて取り組んでいました。</p>
<p><a href="http://www.chatwork.com/ja/" target="_blank"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/tm_first_logo.png" alt="チャットワークロゴ" title="チャットワークロゴ" width="286" height="61" class="alignleft size-full wp-image-1420" /></a></p>
<p>チャットワークはおかげさまで2011年3月1日のリリース以来、<br />
1年で6万ユーザーを突破し現在も順調に成長を続けています。</p>
<p>そして今年の4月1日に、創業から12年使用し続けてきた<br />
「株式会社EC studio」という社名を「ChatWork株式会社」へと<br />
変更することを<a href="http://blog.ecstudio.jp/ec_studio_blog/from_ecstudio_to_chatwork.html" target="_blank">発表しました</a>。<br />
（※エイプリルフールに発表しましたが、本当です＾＾；<br />
変更の実施は6月ごろを予定）</p>
<p>それなりに親しんでいただけていた EC studio という社名を<br />
変更するのは勇気のいることでしたが、チャットワークというサービスには<br />
それだけの可能性があると感じ、不退転の覚悟で決断しました。</p>
<p>今回は、チャットワーク開発の裏側というところで、この2年を振り返りながら<br />
チャットワークを開発するまでの過程や、開発したなかで得たノウハウや<br />
開発手法などをまとめて記事にしたいと思います。</p>
<p><span id="more-1414"></span></p>
<h4>開発のきっかけ</h4>
<p>もともと EC studio では、7年ほど前から社内のコミュニケーションに<br />
Skypeのテキストチャットをメインで使っていました。</p>
<p>Skypeのグループチャット機能は非常に使い勝手がよく、<br />
部署ごとやプロジェクトごとに多数のグループチャットを作って<br />
メーリングリストがわりに運用してきました。</p>
<p>ただ、長年使っているとSkypeチャットには下記のような問題がありました。</p>
<p>・双方がオンラインでないとメッセージが届かない（時間差で届くことも・・）<br />
・チャットログの横断検索ができない<br />
・PCとノートを使い分けるとチャットログが分散する<br />
・スマートフォン版のテキストチャットの使い勝手が非常に悪い</p>
<p>これらの不満をかかえながらも、様々なサービスを試しましたが<br />
代替できるようなサービスはありませんでした。</p>
<p>特に、GoogleからリリースされたGoogle Waveには非常に期待していたのですが、<br />
時系列でメッセージが上から下へ流れていくという考え方がなく、<br />
どこが新しい発言かわかりにくい・雑談が流れずずっと残ってしまう、など<br />
チャットとしての利用は厳しいと判断しました。。。</p>
<p>もう待っていても自分たちの欲しいサービスは出てこないだろうと思い、<br />
それならば自分たちで作ってしまおうと、開発を決心しました。</p>
<h4>チャットワークの開発コンセプト</h4>
<p>チャットワークの開発にあたって、技術的な開発コンセプトは大きく2つありました。</p>
<h5>1. デスクトップアプリと遜色ない操作感を実現する。</h5>
<p>Skypeチャットやメッセンジャーの優れている点の一つが、その軽快な操作性です。</p>
<p>リアルタイムに受信されるメッセージを、多数のチャットルームを<br />
切り替えながら処理していくには、いちいち画面を読み込みなおしていては苦痛です。</p>
<p>GMailの様に、画面の読み込みがないフルAjaxでの開発を選択しました。</p>
<h5>2. マルチデバイス対応。PCでもスマートフォンでも使いやすく。</h5>
<p>チャットワークを開発する大きな動機の一つがモバイル対応です。</p>
<p><a href="http://blog.ecstudio.jp/ec_studio_blog/iphone-allowance-system.html" target="_blank">iPhone支給制度</a>により全スタッフがスマートフォンを持つ環境はあったものの、<br />
Skypeのスマートフォン版はPCで受信したものとログが別管理になっており、<br />
PCで読んだログも未読のままになっていたり、過去のチャットログが遡れなかったりと<br />
ビジネスで使うには実用的ではありませんでした。</p>
<p>データをクラウド化することで、PCでもスマートフォンでも同じデータを閲覧でき、<br />
未読・既読の状態も完全に同期し、使い勝手の良いアプリの開発を目指しました。</p>
<h4>デスクトップアプリと遜色ない操作感を実現するために</h4>
<p>チャットワークはフルAjaxアプリケーションとして開発されているので、<br />
大部分のコードがJavaScriptにより開発されています。</p>
<p>2012年4月現在のコード規模はフレームワーク、ライブラリ、HTML/CSSを<br />
のぞくと大体JavaScriptで約4万行ほど、サーバーサイド(PHP)のコードは<br />
約2万行ほどになります。</p>
<p>さすがに単体アプリケーションとしてこの規模のJavaScriptコードを<br />
書いたのは初めてであり、PHPを中心とした従来のWebアプリケーションの<br />
開発とは全く異なるノウハウが必要でした。</p>
<p>大規模JavaScriptの開発ノウハウについてはあまりWebにも<br />
記事化されたものは少なく、まだベストプラクティスも<br />
固まっていない状態かと思います。</p>
<p>あくまでもEC studioで採用したやり方ですが、うまくいった手法をご紹介します。</p>
<h5>オブジェクト指向で書く</h5>
<p>チャットワークはほとんどの処理をJavaScriptで行う必要があり、<br />
コード規模が大きくなることが予想されたので、<br />
はじめからオブジェクト指向で開発を行いました。</p>
<p>ただ、JavaScriptは文法的に非常に自由な言語なので、<br />
オブジェクト指向といっても様々な書き方ができてしまいます。</p>
<p>ここはかなり悩みましたが、EC studioの技術部で慣れているPHPに<br />
できるだけ近い書き方ができるよう、クラスベースに似せた<br />
コーディング手法を採用することにしました。</p>
<p>例えば、下記のような書き方です</p>
<div class="igBar"><span id="ljavascript-6"><a href="#" onclick="javascript:showCodeTxt('javascript-6'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-6">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> self = <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #009900; font-style: italic;">//自己参照にはself変数を用いる</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//プライベートプロパティ</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> private_prop = ‘aaa’;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//パブリックプロパティ</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; self.<span style="color: #006600;">public_prop</span> = ‘bbb’;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//プライベートメソッド</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> privateMethod = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//something</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//パブリックメソッド</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; self.<span style="color: #006600;">publicMethod</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>self.<span style="color: #006600;">public_prop</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//インスタンスの作成</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> MyObj = <span style="color: #003366; font-weight: bold;">new</span> MyClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>この書き方に統一することで、PHPは得意だけどJavaScriptは経験が<br />
少ないという開発メンバーでも、比較的すぐにJavaScriptのコーディングに<br />
なじむことができました。（習得の容易さを優先しました）</p>
<p>JavaScript独特のクセのあるthisの挙動にも悩まされずすみます。</p>
<p>※この書き方については、過去に「<a href="http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html" target="_blank">PHPエンジニアのためのオブジェクト指向JavaScriptの書き方</a>」という記事でまとめています。</p>
<p>ただ、この書き方では1つのクラスで複数のインスタンスが生成されるような場合に、<br />
メソッドが共通化されずメモリ的にもったいないので、複数のインスタンスが<br />
生成されるようなクラスの場合は、prototype継承を使った書き方を採用しています。</p>
<h5>1ファイル、1クラスで書く</h5>
<p>PHPでもそうしているのですが、JavaScriptでも クラス名.js というファイルに<br />
そのクラス名のクラスを記述しています。</p>
<p><img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/class.png" alt="クラスごとにファイル" title="クラスごとにファイル" width="346" height="254" class="alignleft size-full wp-image-1435" /></p>
<p>こうすることで、ソースコードの見通しが非常によくなります。</p>
<p>ただし、JavaScriptにはPHPのrequireのように、<br />
別のソースコードを動的に読み込む仕組みがありません。<br />
なので、この手法でファイルを作っていくとソースコードの読み込みは</p>
<div class="igBar"><span id="lhtml-7"><a href="#" onclick="javascript:showCodeTxt('html-7'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="html-7">
<div class="html">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class1.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class2.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class3.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class4.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class5.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #009900;"><a target="_blank"&nbsp; href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=”<span style="color: #000066;">text</span>/javascript” <span style="color: #000066;">src</span>=”Class6.js”<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;…..以下続く </div>
</li>
</ol>
</div>
</div>
</div>
<p>
なんてことになってしまいます。<br />
ブラウザはscriptタグのソースを順番に読み込んでいくので、<br />
アプリケーションのロード時間が長くなってしまいます。</p>
<p>これでは問題なので、JavaScriptファイルを1つのファイルにまとめる<br />
ビルドツールを自作し、本番リリース時にはそちらのファイルを<br />
読み込むような仕組みにしました。</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#800000;">&lt;?php</span> <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$local</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#008000;">//開発時 1つずつ読み込む ?&gt;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class1.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class2.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class3.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class4.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class5.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”Class6.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;…..以下続く</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#800000;">&lt;?php</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#0000ff;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#008000;">//本番時 all.jsにまとめる ?&gt;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&lt;script type=”text/javascript” src=”all.js”&gt;&lt;/script&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#800000;">&lt;?php</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#800000;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>こんな感じですね。こうすることで、<br />
開発時にはビルドの手間が不要になり効率よく開発でき、<br />
本番時には読み込みの早いビルド版を使用できます。</p>
<p>ビルドツールの仕組みは単純で、読み込ませたい順番に<br />
JavaScriptファイルの中身を連結していって、<br />
一つのファイルにまとめてやるだけです。（PHPで作りました）</p>
<p>また、まとめるときの最後の処理として、<br />
JavaScript圧縮ツールを使って圧縮処理をかけています。<br />
これによりファイルサイズの削減効果があります。</p>
<p>※自社製ビルドツールの実行結果キャプチャ。圧縮するとサイズが大幅に減ります。<br />
<img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/compiler.png" alt="JSファイルを圧縮" title="JSファイルを圧縮" width="441" height="384" class="alignleft size-full wp-image-1459" /></p>
<p>チャットワークではJavaScript圧縮ツールとして<br />
<a href="https://developers.google.com/closure/compiler/" target="_blank">Google Closure Compiler</a> と <a href="http://developer.yahoo.com/yui/compressor/" target="_blank">YUI Compressor</a> を併用しています。</p>
<p>Google Closure Compilerの方が圧縮率が良いのですが、<br />
後述のモバイル開発ではエラーになってしまう場合があるので、<br />
YUI Compressorも併用しています。</p>
<p>圧縮ツールを使うと、余白・コメントのカットや、<br />
長い変数名などを短くしてくれる為、ファイルサイズを<br />
気にせずコーディングができとても楽になります。</p>
<h5>JavaScriptコードのテスト</h5>
<p>コード規模が大きくなってくると、手動でのテストは限界になり、<br />
自動テストが不可欠になってきます。</p>
<p>PHPであればPHPUnitなどのテストスイートライブラリで<br />
簡単にテストを作ることができますが、ブラウザでの操作が前提の<br />
イベント駆動型であるJavaScriptはテストが困難です。</p>
<p>ブラウザの操作を自動化するにはSeleniumというテストツールが有名で、<br />
チャットワークでもSeleniumを採用しようとしたのですが、<br />
どうしてもうまくいかず断念しました。。<br />
（重かったり、動かなかったり、、、。複数のバージョンを<br />
　かなりがんばって検証したのですが・・・）</p>
<p>というわけで、結局テストツールを自作することにしました。</p>
<p>※テストツールのキャプチャ。<br />
「Run Test」ボタンをクリックすると自動で動作テストします。<br />
<img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/testrunner.png" alt="テストツール" title="テストツール" width="559" height="562" class="alignleft size-full wp-image-1441" /></p>
<p>仕組みは単純で、テストケースを走らせるとjQueryで $(elem).click() のように<br />
イベントをプログラムから順番に叩いて、各種DOMの状況を見てやるようなものです。</p>
<div class="igBar"><span id="ljavascript-9"><a href="#" onclick="javascript:showCodeTxt('javascript-9'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-9">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> TR = <span style="color: #003366; font-weight: bold;">new</span> TestRunner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//ボタンをクリック（ユーザー操作を実行）</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#cw_room_addchat'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">click</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//結果を検証（結果をコンソールUIに表示）</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">TR.<span style="color: #006600;">isEqual</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.ui-widget-overlay'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span>,<span style="color: #CC0000;color:#800000;">1</span>,<span style="color: #3366CC;">'ダイアログが開いたか'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>↑簡単に書くとこんなイメージです。</p>
<p>実際は、テストケースを複数キューに入れて順番に走らせたり、<br />
Ajaxの完了をwaitしたり、alert()やconfirm()で止まらないようにしたり、<br />
XSS脆弱性がないか自動で検証したりなどもろもろ工夫しています。</p>
<h5>コードの自動文法チェック</h5>
<p>JavaScriptは様々なブラウザで挙動が変わるので、<br />
あるブラウザでは問題なく動作するが別のブラウザでは<br />
エラーになって動かなくなる、ということも実際よくあります。</p>
<p>jQueryなどのフレームワークを使えば問題は起きにくいのですが、<br />
よくやってしまうコードとしては</p>
<div class="igBar"><span id="ljavascript-10"><a href="#" onclick="javascript:showCodeTxt('javascript-10'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-10">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; aaa:<span style="color: #CC0000;color:#800000;">1</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; bbb:<span style="color: #CC0000;color:#800000;">2</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>という様に、オブジェクトの要素の最後のカンマを消し忘れると、<br />
IEではエラーになってしまいます。（何度これに泣かされたか・・）</p>
<p>これはJavaScriptの仕様上は問題ないコードのため、<br />
開発エディタなどではエラー検知されませんし、<br />
開発でよく使うFirefoxやChromeでもエラーになりません。</p>
<p>このような問題に対応するため、前述したJavaScriptファイルをひとつのファイルに<br />
まとめる本番用ビルドツールにコードの自動文法チェックの仕組みを実装しました。</p>
<p>※自動コードチェックでエラーを検出した場合のキャプチャ<br />
セミコロン漏れと、最後の余分なカンマを検知。<br />
<img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/linterror.png" alt="JavaScript Lint" title="JavaScript Lint" width="552" height="106" class="alignleft size-full wp-image-1444" /></p>
<p>といってもJavaScriptの構文チェックを自前でやるわけではなく、<br />
1つのファイルにまとめたJavaScriptファイルを、<br />
<a href="http://www.javascriptlint.com/" target="_blank">JavaScript Lint</a>というツールに読み込ませてコードチェックを行います。</p>
<p>このJavaScript Lintはコマンドラインのツールになっているので、<br />
ビルドツールを実行する時にソースコードのパスを渡してやり<br />
チェックをかけています。</p>
<p>これは非常に便利で、多数のバグになりやすいコードを指摘してくれるので<br />
コードの品質を上げることができました。<br />
（デフォルト設定はかなり細かいので、指摘条件をカスタマイズしています）</p>
<p>ビルドツールと組み合わせることで、このチェックに引っかかった場合は<br />
リリースできないようにしています。</p>
<h4>マルチデバイスでの並行開発にチャレンジ</h4>
<p>チャットワークでは、Webアプリケーション版だけではなく、<br />
iPhone・Android・iPadのアプリ版とブラウザ版を同時に開発しています。</p>
<p>EC studioではWebアプリケーションは得意としていましたが、<br />
モバイルアプリなどは全くの専門外で、開発にあたっては大変苦労しました。</p>
<p>はじめは、まずはiPhone版をということで Objective-C を使って<br />
ゼロから開発をはじめていきましたが、モバイル版を担当者が作っているうちに<br />
Web版の仕様や機能がどんどんと変化していってしまうので、<br />
モバイル版の開発がとても追いついていけないという問題にぶつかりました。<br />
(Objective-CよりJavaScriptの方が開発速度が断然速い。。。）</p>
<h5>Titanium Mobileを採用</h5>
<p>そこでどうにかWeb版とモバイル版を同時開発していけないかと思い、<br />
見つけたのが <a href="http://www.appcelerator.com/" target="_blank">Titanium Mobile</a> でした。</p>
<p><a href="http://www.appcelerator.com/" target="_blank"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/titanium_logo.png" alt="Titanium Mobile" title="Titanium Mobile" width="175" height="148" class="alignleft size-full wp-image-1446" /></a></p>
<p>ご存知の方も多いと思いますが、JavaScript で iOS、Android の<br />
アプリケーションを開発できるクロスコンパイラツールです。</p>
<p>チャットワークはフルAjaxで作成されているので、<br />
Titaniumとの相性は抜群です。</p>
<p>試してみたところ、ほとんどそのままのコードで動かすことができました。</p>
<p>MVCアーキテクチャの様に、ロジックやデータ処理を担当するModelクラスと、<br />
表示部分を担当するViewクラスに分割し、Viewの部分だけを<br />
Web版とモバイル版などデバイスで分けて書くようにしました。</p>
<p>※チャットワークのソースコードのフォルダ構造<br />
<img src="http://techblog.ecstudio.jp/wp-content/uploads/2012/04/filetree.png" alt="ファイル構造" title="ファイル構造" width="175" height="273" class="alignleft size-full wp-image-1434" /></p>
<p>Web版のViewでjQueryを使ってHTMLを生成したりDOM操作していた部分を、<br />
モバイル版のViewではTitaniumのUIViewクラスで実装します。</p>
<p>Titaniumの採用により、Web版とモバイル版でコードの大部分(約70%)を<br />
共有できるようになり、大幅に開発効率を上げることができるようになりました。</p>
<p>Web版をバージョンアップすると自動的にモバイル版の機能も<br />
アップデートされていくというのは感動ものです。</p>
<h5>Titanium Mobile 開発の課題</h5>
<p>ただ、Titaniumを使用することで開発効率は上がりましたが、<br />
いろいろな課題にもぶつかりました。</p>
<p>いまではかなり改善されてきてはいますが、Titaniumは新しいツールだけに<br />
非常に多くの不具合、メモリリークや挙動のクセがまだまだ残っており、<br />
それを回避するためにトリッキーなバッドノウハウがたくさん必要です。</p>
<p>また、なんとか動かせたとしても、やはりネイティブの言語で書いた<br />
アプリケーションに比べると、操作感が重かったり動作が不安定になったりします。</p>
<p>これらはアプリケーションレベルでチューニングができるものなのか、<br />
そもそもTitanium本体の問題なのか切り分けが難しく、<br />
いまでも開発に苦慮しています。</p>
<p>このあたりはTitaniumの進化に期待をしたいところですが、<br />
限界も感じていて、ネイティブ版での開発を検討しはじめています。<br />
（開発効率とパフォーマンスとのトレードオフですね・・）</p>
<h4>これからのチャットワーク</h4>
<p>チャットワークの開発をはじめてから、約2年。</p>
<p>社内用のツールをつくろうというところからはじまったこのプロジェクトが、<br />
まさか社名にまでなり、米国法人までつくることになるとは思いませんでした。<br />
（7月より、弊社代表が米国に移住して本格進出いたします）</p>
<p>この淘汰の激しいIT業界で、増資もせず上場もせず、<br />
どこまで世界で戦っていけるかはわかりませんが、<br />
創業してから12年かけてつちかったものをすべてぶつけて、<br />
チャレンジしてみたいと思っています。</p>
<p>そのなかで得たノウハウは、またこのようにブログなどで<br />
随時公開していければと思っています！</p>
<p>今後とも、チャットワークをどうぞよろしくお願いいたします。</p>
<p>アカウントをお持ちでないかたはぜひ<a href="http://www.chatwork.com/ja/" target="_blank">お試しください</a>。無料です。</p>
<p> - 山本正喜（<a href="http://chatwork.com/ec_masaki" target="_blank">ChatWork ID</a>　<a href="https://www.facebook.com/ymasaki" target="_blank">facebook</a>　<a href="https://twitter.com/#!/msk_masaki" target="_blank">twitter</a>)</p>
<h4>※チャットワークの開発者を募集しています</h4>
<p>正直、チャットワークの機能開発が追いついていません・・・。＾＾；</p>
<p>大規模JavaScript開発や、Webと連携するモバイルアプリの開発などに<br />
ご興味がありましたら、ぜひ<a href="http://www.chatwork.com/ja/recruit.html" target="_blank">エントリー</a>を！</p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li>関連する投稿はありません。</li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech/backstage_of_chatwork.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHPエンジニアのためのオブジェクト指向JavaScriptの書き方</title>
		<link>http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html#comments</comments>
		<pubDate>Tue, 22 Jun 2010 11:46:21 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[オブジェクト指向]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=1321</guid>
		<description><![CDATA[JavaScriptはプロトタイプベースのオブジェクト指向言語で、 PHPやJava、C++などのクラスベースのオブジェクトとは 特徴や書き方が大きく異なります。 今回は、PHPでオブジェクト指向開発の経験がある人向けに [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScriptはプロトタイプベースのオブジェクト指向言語で、<br />
PHPやJava、C++などのクラスベースのオブジェクトとは<br />
特徴や書き方が大きく異なります。</p>
<p>今回は、PHPでオブジェクト指向開発の経験がある人向けに、<br />
JavaScriptでのオブジェクト指向プログラミングの書き方を解説します。</p>
<p>サンプルはすべてそのまま実行できるので、<br />
FirebugなどのJavaScriptコンソールで実行して<br />
挙動を確かめながら読み進めるとわかりやすいかと思います。</p>
<p><span id="more-1321"></span></p>
<h4>JavaScript の特徴</h4>
<h5>すべてがオブジェクト</h5>
<p>JavaScriptは完全なオブジェクト指向言語であり、<br />
すべての型の変数、関数はオブジェクトです。<br />
(プロパティやメソッドを持つ)</p>
<div class="igBar"><span id="ljavascript-30"><a href="#" onclick="javascript:showCodeTxt('javascript-30'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-30">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> test = <span style="color: #3366CC;">"文字列"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>test.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//3 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>下記の変数1と変数2の二つの書き方は同じ意味です。<br />
(厳密にはtypeof関数の返り値などが違うが、実用上ほぼ同じ)</p>
<div class="igBar"><span id="ljavascript-31"><a href="#" onclick="javascript:showCodeTxt('javascript-31'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-31">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//数値型</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> num1 = <span style="color: #CC0000;color:#800000;">123</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> num2 = Number<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">123</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//文字列型</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> str1 = <span style="color: #3366CC;">"ABC"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> str2 = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"ABC"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//配列型</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr1 = <span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span>,<span style="color: #CC0000;color:#800000;">2</span>,<span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr2 = <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">arr2<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #CC0000;color:#800000;">1</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">arr2<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #CC0000;color:#800000;">2</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">arr2<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #CC0000;color:#800000;">3</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//オブジェクト型</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj1 = <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj2 = <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">obj2.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">"Masaki"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>この様に、数値、文字列、配列などもオブジェクトとして表現できます。</p>
<h5>関数もオブジェクト</h5>
<p>実は、関数も変数と同じで、</p>
<div class="igBar"><span id="ljavascript-32"><a href="#" onclick="javascript:showCodeTxt('javascript-32'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-32">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> func1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'A'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> func2 = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'A'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">func1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//A</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">func2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//A </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>と変数に代入する形で表現できます。<br />
(関数は、"定義"ではなく実体を持つ"変数")</p>
<p>function(){} の様な、関数名を書かない関数の定義方法を<br />
無名関数(またはラムダ関数)といいます。<br />
変数に代入するときには無名関数を使用します。</p>
<p>また、PHPでは可能なこういった構文はエラーになります</p>
<div class="igBar"><span id="ljavascript-33"><a href="#" onclick="javascript:showCodeTxt('javascript-33'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-33">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'A'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">test = <span style="color: #3366CC;">'ABC'</span>; <span style="color: #009900; font-style: italic;">//ここで関数を上書きしている！</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//エラー </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>この例でもわかる通りJavaScriptでは<br />
関数の定義後に上書き(再定義)や書き換え、削除などが可能です。</p>
<p>さらに、変数と同じなのでこんなこともできます。</p>
<div class="igBar"><span id="ljavascript-34"><a href="#" onclick="javascript:showCodeTxt('javascript-34'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-34">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'A'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> do_func<span style="color: #66cc66;">&#40;</span>func<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; func<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//test関数を引数に</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">do_func<span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//その場で無名関数を作って引数に</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">do_func<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'B'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>オブジェクトと同じように引数として渡すことも、返り値として返すこともできます。</p>
<h5>内部構造を持ったオブジェクトのコピーは参照渡し</h5>
<p>JavaScriptではPHPとは違い、配列に対しても<br />
参照渡しでのコピー(シャローコピー)になります。</p>
<div class="igBar"><span id="ljavascript-35"><a href="#" onclick="javascript:showCodeTxt('javascript-35'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-35">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr1 = <span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span>,<span style="color: #CC0000;color:#800000;">1</span>,<span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr2 = arr1;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">arr2<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #CC0000;color:#800000;">5</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//コピー元の配列も書き換わる！</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>arr1<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//5</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj2 = obj;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">obj2.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">"Yamamoto"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//コピー元のオブジェクトも書き換わる！</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>obj.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//Yamamoto </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>メソッドのないオブジェクト(PHPの連想配列)でも<br />
シャローコピーになるので注意しましょう。</p>
<p>完全なコピー(ディープコピー)をするには</p>
<div class="igBar"><span id="ljavascript-36"><a href="#" onclick="javascript:showCodeTxt('javascript-36'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-36">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr1 = <span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span>,<span style="color: #CC0000;color:#800000;">1</span>,<span style="color: #CC0000;color:#800000;">2</span><span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> arr2 = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> arr1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; arr2<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = arr1<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj1 = <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj2 = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> obj1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; obj2<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = obj1<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
の様に、forなどでコピーすればOK。<br />
※ただし、多次元配列の様なものは再帰的に処理する必要あり</p>
<p>※for (X in OBJ) の構文はPHPのforeach同様に使えてとても便利です！</p>
<h5>関数とスコープ</h5>
<p>PHPと同様に、JavaScriptでも関数内を別スコープにすることができますが、<br />
スコープの扱い方が大きく異なるので注意が必要です。</p>
<p>グローバル変数は、関数内でもそのまま参照できます。</p>
<div class="igBar"><span id="ljavascript-37"><a href="#" onclick="javascript:showCodeTxt('javascript-37'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-37">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//グローバル変数</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> test = <span style="color: #3366CC;">"Masaki"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> myfunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">myfunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Masaki </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>関数スコープ内でグローバル変数の内容を変更した場合も、<br />
グローバル変数が書き変わります。</p>
<p>ローカル変数を作成したい場合は、 <strong>var</strong> を使います。</p>
<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showCodeTxt('javascript-38'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-38">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//グローバル変数</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> test = <span style="color: #3366CC;">"Masaki"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> myfunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> test = <span style="color: #3366CC;">'Yamamoto'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">myfunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Yamamoto</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//Masaki </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>var で宣言された変数は、宣言されたスコープのローカル変数となります。<br />
var で宣言しない変数はグローバル変数になります。</p>
<p>ただ、関数の引数は例外で、varをつけなくてもローカル変数扱いになります。</p>
<h4>JavaScriptにおけるクラス</h4>
<p>オブジェクト指向におけるクラスの基本的な機能は</p>
<p>    * プロパティ<br />
    * メソッド<br />
    * コンストラクタ<br />
    * アクセス修飾子 (public/private)<br />
    * 継承</p>
<p>などですが、JavaScriptでもすべてできます。</p>
<p>JavaScriptにはクラスは存在せず、オブジェクトと<br />
関数を組み合わせて同様の機能を実装します。</p>
<p>通常PHPなどクラスベースのオブジェクト指向言語では、</p>
<p>クラス → インスタンス</p>
<p>と、クラスという"型"からインスタンスという"実体"を生成しますが、<br />
プロトタイプベースのオブジェクト指向言語には"クラス"という<br />
考え方は存在せず、定義＝実体となります。(インスタンスしかない)</p>
<p>JavaScriptでは new という構文があり、クラスからインスタンス化している<br />
ように見えますが、実際の処理は元となるオブジェクトを<br />
コピー(+継承)して作られます。</p>
<p>インスタンスしか存在しないのにどう継承などを実装するのかなどは、<br />
追って説明していきます。</p>
<h5>プロパティ</h5>
<p>JavaScriptでのプロパティ定義は、PHPの様に宣言は必要ありません。</p>
<div class="igBar"><span id="ljavascript-39"><a href="#" onclick="javascript:showCodeTxt('javascript-39'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-39">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Masaki'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">company</span> = <span style="color: #3366CC;">"EC studio"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>インスタンス化した後で追加することもできますし、</p>
<div class="igBar"><span id="ljavascript-40"><a href="#" onclick="javascript:showCodeTxt('javascript-40'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-40">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; company:<span style="color: #3366CC;">"EC studio"</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>と、シンタックスシュガー(簡易記法)を使って簡単に定義することもできます。</p>
<h5>メソッド</h5>
<p>メソッドもプロパティとほぼ同様に記述できます。</p>
<div class="igBar"><span id="ljavascript-41"><a href="#" onclick="javascript:showCodeTxt('javascript-41'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-41">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello!'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>シンタックスシュガーを使うと</p>
<div class="igBar"><span id="ljavascript-42"><a href="#" onclick="javascript:showCodeTxt('javascript-42'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-42">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; hello:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>こう書けます。</p>
<p>プロパティと組み合わせると、</p>
<div class="igBar"><span id="ljavascript-43"><a href="#" onclick="javascript:showCodeTxt('javascript-43'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-43">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; company:<span style="color: #3366CC;">"EC studio"</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; hello:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//Hello </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>こう書けます。</p>
<p>メソッド内で自分自身のプロパティやメソッドを参照するには、<strong>this</strong> を使います。</p>
<div class="igBar"><span id="ljavascript-44"><a href="#" onclick="javascript:showCodeTxt('javascript-44'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-44">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">name</span>:<span style="color: #3366CC;">"Masaki"</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; company:<span style="color: #3366CC;">"EC studio"</span>,</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; hello:<span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello '</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//Hello Masaki </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
これでかなりクラスに近くなりましたね。</p>
<h5>コンストラクタ</h5>
<p>オブジェクトをインスタンス化する時に実行されるメソッドであるコンストラクタは、<br />
下記の様に実装できます。(厳密には、インスタンス化ではなくコピー+継承)</p>
<div class="igBar"><span id="ljavascript-45"><a href="#" onclick="javascript:showCodeTxt('javascript-45'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-45">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #003366; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Hello </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>new Object() ではなく、関数をnewの対象にすることで<br />
その関数の中身をnew実行時に実行できます。</p>
<p>コンストラクタに返り値を指定してもオブジェクト型以外は無視されます。<br />
コンストラクタの返り値がオブジェクト型の場合はそのオブジェクトが<br />
newで生成されるオブジェクトになります。</p>
<p>このコンストラクタの仕組みを使うと、newした時に<br />
自動的にプロパティやメソッドを定義できる、<br />
とても PHPのクラスに近い書き方ができます。</p>
<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showCodeTxt('javascript-46'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-46">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Masaki'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">company</span> = <span style="color: #3366CC;">'EC studio'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hello</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>message + <span style="color: #3366CC;">' '</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//helloを実行</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #003366; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Hello Masaki</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Good morning'</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Good morning Masaki </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>関数はObjectでもあるので、this が使えます。<br />
コンストラクタの処理の中に thisへの代入を含めることで、<br />
初期の定義を関数内に入れてしまうことができます。</p>
<p>※コンストラクタの引数である messageと、helloメソッドの引数のmessageが<br />
同じ変数名になっていますが、helloメソッドのものは関数定義の中での<br />
引数名なので、別のものと認識されます。</p>
<h5>アクセス修飾子(public/private)</h5>
<p>JavaScriptにはアクセス修飾子はありませんが、コンストラクタの例で挙げた<br />
関数でのオブジェクト定義を使えば、プロパティやメソッドにアクセス制限を<br />
かけることができます。</p>
<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showCodeTxt('javascript-47'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-47">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> self = <span style="color: #000066; font-weight: bold;">this</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//public</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Masaki'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">company</span> = <span style="color: #3366CC;">'EC studio'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//private</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> separator = <span style="color: #3366CC;">','</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//public</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hello</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>message + separator + getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//private</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> getName = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> self.<span style="color: #000066;">name</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> object = <span style="color: #003366; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>object.<span style="color: #006600;">separator</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//undefined</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">object.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//エラー </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>関数内にvarを使ってローカル変数による変数や関数にすることで、<br />
外からは参照出来ない privateなプロパティ、メソッドを作ることができます。<br />
(protectedなプロパティ、メソッドは作成できません)</p>
<p>private なプロパティ、メソッドは this を使わず<br />
var を使ってローカル変数として参照します。</p>
<p>privateなメソッドを作るときの注意として、<br />
メソッドのfunction定義内では <strong>this は使えません。</strong><br />
その場合の対策として、this を self というローカル変数にコピーして使用しています。<br />
※privateなメソッドのfunction内のthisは、その関数自体を指すため。</p>
<p>この様に、this はそのメソッドの呼び出し元の主体に応じて変わってしまうので、<br />
その場合に対応するために<strong>var self = this</strong> をコンストラクタで<br />
あらかじめ定義しておくと便利です。</p>
<p>※privateなメソッドの場合の他に、イベントハンドラによって<br />
メソッドが実行された場合でも this が変わってしまいます。<br />
(クリック処理や、Ajax処理など)</p>
<p>これでプロパティ、メソッド、コンストラクタを実現できました。</p>
<p>JavaScriptには他にも様々なオブジェクトの書き方があり、<br />
それぞれにメリット・デメリットがありますが、<br />
クラスベースに文法が比較的近いこの記述方法が私のおすすめです。</p>
<h5>継承</h5>
<p>JavaScriptのオブジェクトには prototype という予約語のプロパティがあり、<br />
そのprototypeに親となるオブジェクトを代入することで継承を実現できます。<br />
prototype = 型 という意味。元となる型のオブジェクトを指定できます。</p>
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showCodeTxt('javascript-48'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-48">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//親オブジェクト</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> self = <span style="color: #000066; font-weight: bold;">this</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//public</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Masaki'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">company</span> = <span style="color: #3366CC;">'EC studio'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//private</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> separator = <span style="color: #3366CC;">','</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//public</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hello</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>message + separator + getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//private</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> getName = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> self.<span style="color: #000066;">name</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//子オブジェクト</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> MyObjectChild<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//プロパティをオーバーライド</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Yamamoto'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//privateはオーバーライドできない</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> separator = <span style="color: #3366CC;">'|'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//メソッドを追加</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">thankyou</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">hello</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Thank you'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//プロトタイプ(継承元)を指定</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">MyObjectChild.<span style="color: #006600;">prototype</span> = <span style="color: #003366; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> mychild = <span style="color: #003366; font-weight: bold;">new</span> MyObjectChild<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//子オブジェクトの中で、親オブジェクトのhelloメソッドが実行できる</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">mychild.<span style="color: #006600;">thankyou</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">//Thank you,Yamamoto </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>privateなメンバ(var で定義したもの)はオーバーライドできません。<br />
これは、JavaScriptにおける var の挙動を理解していれば<br />
何が起きているかがわかるかと思います。<br />
(親の関数のローカル変数なので上書きできない)</p>
<hr />
JavaScriptのオブジェクト指向はPHPと比較すると複雑に見えますが、<br />
その挙動や書き方を理解してしまえば、とても柔軟かつシンプルに記述でき、<br />
大規模なシステム開発も十分可能なものだと思います。</p>
<p>オブジェクト指向でのJavaScript開発の参考になれば幸いです。</p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/news/vol5.html" title="週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ (2008/02/22 金曜日)">週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech/attractive-jquery.html" title="プログラマ視点でみたjQueryの魅力 (2010/03/09 火曜日)">プログラマ視点でみたjQueryの魅力</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/phptail.html" title="jQueryとPHPでLinuxのtailコマンドを実装する (2009/10/06 火曜日)">jQueryとPHPでLinuxのtailコマンドを実装する</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/google-ajax-libraries-api-updated.html" title="Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に (2009/01/19 月曜日)">Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>プログラマ視点でみたjQueryの魅力</title>
		<link>http://techblog.ecstudio.jp/tech/attractive-jquery.html</link>
		<comments>http://techblog.ecstudio.jp/tech/attractive-jquery.html#comments</comments>
		<pubDate>Mon, 08 Mar 2010 15:00:05 +0000</pubDate>
		<dc:creator>津留 奨史</dc:creator>
				<category><![CDATA[EC studio 技術部]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=987</guid>
		<description><![CDATA[jQueryは、リッチなUIを簡単な記述で実装できます。 デザイナーにも理解しやすい仕様（そこも魅力の一つ！）なので、 いまやデザイナー向けにjQueryを説明するブログもたくさんありますね。 ただ、完全にデザイナー向け [...]]]></description>
			<content:encoded><![CDATA[<p>jQueryは、リッチなUIを簡単な記述で実装できます。</p>
<p>デザイナーにも理解しやすい仕様（そこも魅力の一つ！）なので、<br />
いまやデザイナー向けにjQueryを説明するブログもたくさんありますね。</p>
<p>ただ、完全にデザイナー向けのライブラリなのか？というと、そうでもなく<br />
デザイナーだけではなくプログラマ視点から見ても魅力があるライブラリだと<br />
言えるでしょう。</p>
<p>今回はプログラマ視点でみたjQueryの魅力と感じた点を簡単にまとめてみます。<br />
<span id="more-987"></span></p>
<h4>コードがスマートになる</h4>
<p>なんといってもココに魅力を感じます！<br />
細かく言うと以下のものがあげられます。</p>
<h5>・要素の操作もシンプル簡単！</h5>
<p>jQueryはセレクタを利用して要素を簡単に取得し、<br />
エフェクトの追加やDOMの操作ができます。<br />
（CSSとほぼ同様のセレクタを指定して<br />
簡単にDOMの操作ができるところがデザイナーにも優しいですね！）</p>
<p>これは、記述方法をみていただければよくわかりますので<br />
通常の書き方とjQueryを利用した書き方で比較してみます。</p>
<div class="igBar"><span id="lcode-56"><a href="#" onclick="javascript:showCodeTxt('code-56'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-56">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span> src=<span style="color:#CC0000;">"jquery-1.4.1.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;h3&gt;ホームページ支援サービス一覧 <span style="color:#006600; font-weight:bold;">&#40;</span>jQueryで色を変更<span style="color:#006600; font-weight:bold;">&#41;</span>&lt;/h3&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;ul id=<span style="color:#CC0000;">"hpbackup"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;Web Analyst&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;HP売上プラス&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;EC DVD&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/ul&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;h3&gt;おすすめソフトウェア一覧 <span style="color:#006600; font-weight:bold;">&#40;</span>通常のjavascriptで色を変更<span style="color:#006600; font-weight:bold;">&#41;</span>&lt;/h3&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;ul id=<span style="color:#CC0000;">"software"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;ESET Smart Security&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;ESET NOD32アンチウイルス&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;MindManager&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;Camtasia&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;li&gt;Snagit&lt;/li&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/ul&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//jQueryで特定の&lt;ul&gt;タグ以下のテキストカラーを変更</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#hpbackup li'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'color'</span>,<span style="color:#CC0000;">'#00f'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//普通のjavascriptで特定の&lt;ul&gt;タグ以下のテキストカラーを変更</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">for <span style="color:#006600; font-weight:bold;">&#40;</span>i in document.<span style="">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'software'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">childNodes</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; if <span style="color:#006600; font-weight:bold;">&#40;</span>document.<span style="">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'software'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">childNodes</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="">tagName</span> == <span style="color:#CC0000;">'LI'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; document.<span style="">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'software'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">childNodes</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="">style</span>.<span style="">color</span> = <span style="color:#CC0000;">'#0f0'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
上記のスクリプトは、&lt;li&gt;のテキストカラーを変更しているものです。</p>
<p>比べていただくとわかるように<br />
通常の書き方で書くよりもjQueryを利用した書き方のほうが<br />
明らかにスマートで見やすい書き方なのではないでしょうか！？</p>
<h5>・1行でAjax！</h5>
<p>なんと１行でAjaxが書けます！</p>
<p>例えば、<br />
同ディレクトリにある sample.phpの出力結果をidがhogeのタグ内に読み込む場合<br />
以下のように記述します。</p>
<div class="igBar"><span id="lcode-57"><a href="#" onclick="javascript:showCodeTxt('code-57'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-57">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;div id=<span style="color:#CC0000;">"hoge"</span>&gt;&lt;/div&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#hoge'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'./sample.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
これだけです！</p>
<p>Ajaxをサポートしているライブラリは<a href="http://www.extjs.co.jp/" target="_blank">Ext JS</a>や<a href="http://www.prototypejs.org/" target="_blank">Prototype</a>などがありますが、<br />
いずれもjQueryほどシンプルに実装とはいきません。</p>
<p>本当にこの機能だけでも十分魅力的です。</p>
<h5>・メソッドチェーンの利用で重複のないコードに</h5>
<p>jQueryを使うと何度も同じ要素を指定する必要がなくなります。</p>
<p>例えば、<br />
指定したタグの中にある文字列と文字色を変更する場合です。</p>
<div class="igBar"><span id="lcode-58"><a href="#" onclick="javascript:showCodeTxt('code-58'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-58">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;span id=<span style="color:#CC0000;">"hoge"</span>&gt;メソッドチェーン&lt;/span&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//毎回、要素の指定をした書き方</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#hoge'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'color'</span>,<span style="color:#CC0000;">'#FF0000'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<span style="color:#FF9933; font-style:italic;">//色を赤に変更</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#hoge'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">html</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'jQuery'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<span style="color:#FF9933; font-style:italic;">//「jQuery」という文字をタグ内に表示(innerHTMLと同じ)</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//メソッドチェーンを利用した書き方</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#hoge'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'color'</span>,<span style="color:#CC0000;">'#FF0000'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">html</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'jQuery'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上記のように</p>
<p>要素を取得→イベントやスタイルを指定<br />
要素を取得→イベントやスタイルを指定<br />
・・・</p>
<p>といったように、毎回要素を取得して記述する方法より</p>
<p>要素を取得<br />
　→イベントやスタイルを指定<br />
　→イベントやスタイルを指定<br />
・・・</p>
<p>という形で、ひとつの要素を取得したら<br />
取得した要素に対して複数の指定をまとめて行うほうが効率的です。<br />
（実際、処理速度の改善にもなります）<br />
そしてコードもスマート！</p>
<p>同じ処理ならjQueryのメソッドチェーンを利用した記述をおすすめします！</p>
<h4>readyが便利すぎる！</h4>
<p>jQueryのスクリプトを見るとよくreadyが記述されています。<br />
以下のような記述です。</p>
<div class="igBar"><span id="lcode-59"><a href="#" onclick="javascript:showCodeTxt('code-59'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-59">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">ready</span><span style="color:#006600; font-weight:bold;">&#40;</span>function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">//この中に処理を記述</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
このreadyが実行されるタイミングはDOMの操作を行いやすいので、<br />
実はとても便利なメソッドです。</p>
<p>とはいっても、文字だけで動きの説明は難しいので<br />
具体的に、コードとあわせて説明します。</p>
<div class="igBar"><span id="lcode-60"><a href="#" onclick="javascript:showCodeTxt('code-60'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-60">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span> src=<span style="color:#CC0000;">"jquery-1.4.1.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#FF9933; font-style:italic;">//サイズ変更</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#book1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">width</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">370</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">height</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">600</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;img id=<span style="color:#CC0000;">"book1"</span> src=<span style="color:#CC0000;">"http://blog.ecstudio.jp/wp-content/uploads/2010/01/book-185x300.jpg"</span> style=<span style="color:#CC0000;">"margin: 10px"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
上記の例では#book1はサイズが変更されません。</p>
<p>これは、HTMLのドキュメント構造が生成される順番を意識せずに<br />
スクリプトを記述しているからです。<br />
上記スクリプトの場合、&lt;img&gt;タグのドキュメント構造が生成されていない状態で&lt;head&gt;タグが読み込まれ、&lt;img&gt;タグに対する処理をしようとしたため<br />
後で生成された&lt;img&gt;タグはサイズが変わらなかったのです。</p>
<p>この問題をreadyを使わずに解決するためには、<br />
onloadイベントを利用することになります。<br />
しかし、onloadを利用しても問題点はあります。</p>
<p>まずはコードをみていただきましょう。</p>
<div class="igBar"><span id="lcode-61"><a href="#" onclick="javascript:showCodeTxt('code-61'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-61">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span> src=<span style="color:#CC0000;">"jquery-1.4.1.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;function load<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#book1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">width</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">370</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">height</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">600</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body onload=<span style="color:#CC0000;">"load();"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;img id=<span style="color:#CC0000;">"book1"</span> src=<span style="color:#CC0000;">"http://blog.ecstudio.jp/wp-content/uploads/2010/01/book-185x300.jpg"</span> style=<span style="color:#CC0000;">"margin: 10px"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
上記のようにすれば、たしかに画像のサイズは変更されて表示されます。<br />
しかし、実行すれば分かりますが一度画像がレンダリングされてしまったあとの<br />
タイミングでonloadイベントは動く為、<br />
一瞬ですが表示の時にサイズ変更前の画像が見えてしまいます。<br />
(※キャッシュによって２回目からははじめから大きな画像が出ている場合もあります）</p>
<p>この問題を解決するのがjQueryのreadyです。<br />
先に書いたスクリプトを少しだけ変更してreadyを使った書き方に変えてみます。</p>
<div class="igBar"><span id="lcode-62"><a href="#" onclick="javascript:showCodeTxt('code-62'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-62">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span> src=<span style="color:#CC0000;">"jquery-1.4.1.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;script type=<span style="color:#CC0000;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;$<span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">ready</span><span style="color:#006600; font-weight:bold;">&#40;</span>function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'#book1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">width</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">370</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">height</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">600</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&lt;img id=<span style="color:#CC0000;">"book1"</span> src=<span style="color:#CC0000;">"http://blog.ecstudio.jp/wp-content/uploads/2010/01/book-185x300.jpg"</span> style=<span style="color:#CC0000;">"margin: 10px"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
こちらのコードを実行してみるといかがでしょうか？<br />
はじめから大きな画像が出ているのではないでしょうか。<br />
もちろん動作はキャッシュを消しても変わりません。<br />
この挙動の秘密はreadyとonloadの実行タイミングの違いにあります。</p>
<p>onloadとreadyの実行タイミングの違いを先にざっくり示します。</p>
<p>●onloadの場合<br />
１、ドキュメント構造完成<br />
↓<br />
２、画像読み込み・レンダリング<br />
↓<br />
３、onloadイベントの実行（今回は画像の操作）</p>
<p>●readyの場合<br />
１、ドキュメント構造完成<br />
↓<br />
２、readyの実行（今回は画像の操作）<br />
↓<br />
３、画像読み込み・レンダリング</p>
<p>このように<br />
onloadの実行タイミングはすべてのドキュメント構造が完成して、<br />
画像の読み込みが行われた後になります。<br />
つまりこの読み込み時点では、画像は小さいままのものになっています。<br />
ところがreadyの場合、ドキュメント構造が完成した直後に呼ばれます。<br />
もちろん画像が読み込まれる前になります。<br />
そのため、先にjavascriptで画像のサイズを変更した後に画像を読みこむので、<br />
はじめから画像はサイズが変更されているものが表示されるように見えるのです。</p>
<p>以上のことから<br />
readyを利用することでドキュメント構造の生成順番をそれほど意識する必要がなく<br />
&lt;head&gt;タグ内にもDOMの操作を行うスクリプトを記述できるのです。</p>
<p>実はこのreadyの実行タイミングはDOMContentLoadedというイベントを利用すればFireFoxなどでは同じ動きのものが実現できます。<br />
しかし、Internet ExplorerではDOMContentLoadedが存在しません。<br />
そのため、同じような挙動をさせるには別の方法で実装する必要があります。</p>
<p>jQueryはブラウザ間の違いを吸収してくれるクロスブラウザなライブラリなので、<br />
どのブラウザでもreadyを利用するだけで同様の挙動を実現することができます。<br />
（ブラウザに依存しないところもまたひとつのメリットであると言えるでしょう。）</p>
<p>readyを利用するとブラウザの問題やドキュメント構造の構築タイミングを<br />
考慮する必要がなくなるので安心してスクリプトを記述できます！＾＾</p>
<p>以上、触ってみてプログラマ視点で魅力的に感じた部分を上げてみました。<br />
もし、まだ利用されていないという方でしたらぜひ使ってみてはいかがでしょうか！？</p>
<p>こちらでダウンロードできます。<br />
<a href="http://jquery.com/" target="_brank">http://jquery.com/</a></p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/tech-tips/phptail.html" title="jQueryとPHPでLinuxのtailコマンドを実装する (2009/10/06 火曜日)">jQueryとPHPでLinuxのtailコマンドを実装する</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/vol5.html" title="週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ (2008/02/22 金曜日)">週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html" title="PHPエンジニアのためのオブジェクト指向JavaScriptの書き方 (2010/06/22 火曜日)">PHPエンジニアのためのオブジェクト指向JavaScriptの書き方</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/google-ajax-libraries-api-updated.html" title="Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に (2009/01/19 月曜日)">Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech/attractive-jquery.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>超高速でtwitterにキャプチャ画像をつぶやく仕組みを作ってみました</title>
		<link>http://techblog.ecstudio.jp/tech-tips/ultraquick-capture-for-twitter.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/ultraquick-capture-for-twitter.html#comments</comments>
		<pubDate>Mon, 22 Feb 2010 12:43:15 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[PHP Tips]]></category>
		<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[gyazo]]></category>
		<category><![CDATA[twitpic]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=1134</guid>
		<description><![CDATA[twitterを使っていて、デスクトップのキャプチャ画像を つぶやきたい時がよくありませんか？ [キャプチャ画像の例] 通常だとtwitterにつぶやくまではこんな手順ではないでしょうか。 1.キャプチャしたい状態を準備 [...]]]></description>
			<content:encoded><![CDATA[<p>twitterを使っていて、デスクトップのキャプチャ画像を<br />
つぶやきたい時がよくありませんか？</p>
<p>[キャプチャ画像の例]<br />
<img class="alignnone size-medium wp-image-1136" title="キャプチャサンプル" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/capture_sample-300x254.png" alt="キャプチャサンプル" width="300" height="254" /></p>
<p>通常だとtwitterにつぶやくまではこんな手順ではないでしょうか。</p>
<p>1.キャプチャしたい状態を準備<br />
2.PrintScreenなどでキャプチャ<br />
3.画像編集ソフトで切り抜いてファイル保存<br />
4.<a href="http://twitpic.com/" target="_blank">twitpic</a>を開いてログイン<br />
5.<a href="http://twitpic.com/" target="_blank">twitpic</a>でファイルアップロード<br />
6.twitterでURLをつぶやく</p>
<p>うーん、手間です。。</p>
<p>キャプチャソフトを使ったり、twitterクライアントを<br />
使えば少しは楽になりますが、いちいちファイルを<br />
保存してアップというのがそもそも面倒です。</p>
<p>twitterなのでもっと気軽にさくっとつぶやきたいところです。</p>
<p>というわけでいろいろとツールを組み合わせて、<br />
超高速でtwitterへキャプチャ画像をアップする仕組みを<br />
作ってみました。</p>
<h4>今回の完成品</h4>
<p>ショートカットキーをたたくと</p>
<p><img class="alignnone size-full wp-image-1137" title="カーソルが十字に" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/cursor_cross.png" alt="カーソルが十字に" width="413" height="336" /></p>
<p>カーソルが十字にかわり、</p>
<p><img class="alignnone size-full wp-image-1138" title="範囲指定" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/cursor_area.png" alt="範囲指定" width="411" height="335" /></p>
<p>ドラッグして範囲指定すると</p>
<p><img class="alignnone size-full wp-image-1139" title="twitpicへアップロード" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/uploaded.png" alt="twitpicへアップロード" width="403" height="472" /></p>
<p>即座にtwitpicにアップされ、画像のプレビュー画面が表示されます。<br />
(http://twitpic.com/xxxxx をクリックするとtwitpicが開きます)</p>
<p>ここから、<strong>「この画像をツイートする」</strong>をクリックすると、</p>
<p><img class="alignnone size-full wp-image-1140" title="twitterへpost" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/twitter_post.png" alt="twitterへpost" width="569" height="152" /></p>
<p>twitterが開き、そのURLがすでに入力された状態になります。<br />
あとはコメントを書いてpostすればok！</p>
<p>というものを作ってみました。<br />
キャプチャをtwitterへつぶやくまでにかかる時間は<strong>たった数秒</strong>です。</p>
<p>デスクトップのキャプチャソフトと、<a href="http://twitpic.com/api.do" target="_blank">twitpicのAPI</a>を連携させて実現しています。<br />
スクリプトを改造すればtwitpic以外のサービス(tumblr,flickrなど)に<br />
アップしたりなども自在なので、なかなか面白い仕組みが<br />
できたのではないかと思います。 <img src='http://techblog.ecstudio.jp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>では、さっそく作り方を説明します！</p>
<p><span id="more-1134"></span></p>
<h4>仕組み</h4>
<p>仕組みとしては、</p>
<p>・キャプチャソフトのGyazo<br />
・twitpicアップロード用の自作PHPスクリプト</p>
<p>を組み合わせています。</p>
<p>■Gyazoについて</p>
<p>Gyazoは</p>
<p><a href="http://gyazo.com/ja" target="_blank">http://gyazo.com/ja</a></p>
<p>で配布されている、スクリーンショットの超簡単キャプチャソフトです。</p>
<p>キャプチャしたい範囲を選ぶだけで、<br />
即座にサーバーへ画像をアップロードしてくれます。</p>
<p>ただ、Gyazoそのままでは</p>
<p>・アップロードされるURLの文字数がとても長い<br />
・アップロードした画像は(いまのところ)削除出来ない<br />
・アップロードされるサーバーが固定(gyazoのサーバー)</p>
<p>など少しtwitterでは使いづらい部分があるので、<br />
そこを自作のPHPスクリプトで補います。</p>
<p>また、今回はGyazoを改造した<a href="http://d.hatena.ne.jp/nvsofts/20090321/1237619040" target="_blank">Gyazowin+</a>を使用します。</p>
<p>Gyazowin+は本家版に対し、</p>
<p>・アップロード先の変更<br />
・Basic認証・SSLの対応<br />
・確認ダイアログの表示</p>
<p>の機能が追加されており、これらの機能を使って<br />
twitpicへのアップロードを実現します。</p>
<p>■PHPスクリプトについて</p>
<p>PHPスクリプトでは、Gyazowin+からアップロードされた<br />
ファイルを受け取り、そのままtwitpicのAPIを使って<br />
アップロードします。</p>
<p>twitpicへアップロードした後は、プレビュー画面(下記)を表示して、</p>
<p><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/uploaded-256x300.png" alt="twitpicへアップロード" title="twitpicへアップロード" width="256" height="300" class="alignnone size-medium wp-image-1139" /></p>
<p>画像を確認後twitterへすぐにツイートできるようにしています。<br />
(失敗キャプチャもあるかもなので、一度確認画面を挟んでいます)</p>
<h4>作り方1:PHPスクリプトをアップロード</h4>
<p>まずPHPスクリプトをダウンロードしてください。</p>
<p><a href="http://techblog.ecstudio.jp/files/gyazopic.zip"> gyazopic.php (zipファイルをダウンロード)</a></p>
<p>ダウンロードしたら解凍し、スクリプトの頭にある<br />
下記の設定部分を編集してください。</p>
<div class="igBar"><span id="lphp-66"><a href="#" onclick="javascript:showCodeTxt('php-66'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-66">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * 設定</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> */</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">//twitterのアカウント情報</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#663333;">$username</span> = <span style="color:#ff00ff;">''</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#663333;">$password</span> = <span style="color:#ff00ff;">''</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>$usernameにtwitterユーザー名、<br />
$passwordにパスワードを入れます。</p>
<p>(例)</p>
<div class="igBar"><span id="lphp-67"><a href="#" onclick="javascript:showCodeTxt('php-67'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-67">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * 設定</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> */</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">//twitterのアカウント情報</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#663333;">$username</span> = <span style="color:#ff00ff;">'msk_masaki'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#663333;">$password</span> = <span style="color:#ff00ff;">'passwordxxxx'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>編集後、自分のサーバーの好きな箇所へ<br />
アップロードしてください。</p>
<h4>作り方2:Gyazowin+をダウンロード</h4>
<p>次に、配布サイトよりgyazowin+をダウンロードします。</p>
<p><a href="http://d.hatena.ne.jp/nvsofts/20090321/1237619040" target="_blank">http://d.hatena.ne.jp/nvsofts/20090321/1237619040</a></p>
<p><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/427b049727ee5f865c790b71cadacbd7.png" alt="gyazowin+ ダウンロード" title="gyazowin+ ダウンロード" width="415" height="425" class="alignnone size-full wp-image-1145" /></p>
<p>好きなフォルダへ解凍すればokです。</p>
<p>次に、解凍したフォルダの中にある<br />
gyazowin+.ini をメモ帳などで編集します。</p>
<div class="igBar"><span id="lhtml-68"><a href="#" onclick="javascript:showCodeTxt('html-68'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="html-68">
<div class="html">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">[gyazowin+]</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">upload_server=www.test.com</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">upload_path=/folder/gyazopic.php</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">use_ssl=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">ssl_check_cert=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">use_auth=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">auth_id=</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">auth_pw=</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">up_dialog=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">copy_url=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">copy_dialog=no</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">open_browser=yes </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>upload_serverに、PHPスクリプトを設置するドメイン名、<br />
upload_pathに、ドメイン以降のファイルパスを記述します。</p>
<p>これで設定は完了です！</p>
<h4>ショートカットキーを設定する</h4>
<p>gyazowin+.exe を右クリックしてショートカットを作成し、<br />
ショートカットを右クリック→プロパティを開きます。</p>
<p><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/shortcut.png" alt="ショートカットキー" title="ショートカットキー" width="342" height="371" class="alignnone size-full wp-image-1146" /></p>
<p>ショートカットキーに、好きなキーをセットします。</p>
<p>あとは、そのショートカットキーを押せば、<br />
カーソルが十字に変わり、範囲指定すれば<br />
twitpicへと即座にアップロードされます！<br />
(※キャンセルしたい場合は右クリック)</p>
<h4>おまけ：活用方法</h4>
<p>今回使用したGyazoは、即座にキャプチャファイルをサーバーへ<br />
POSTすることができるので、様々な活用方法が考えられるのではないかと思います。</p>
<p>Google Documentへとアップロードしたり、Blogに即postしたり、<br />
tumblrに投げたり、flickrへ保存したり、、、、</p>
<p>社内のサポートツールにも使えるかもしれませんね。<br />
EC studioではこのGyazoを使ってサイト制作やシステム開発の時の<br />
ラフ画像や、トラブル発生時のキャプチャなどをやりとりしています。<br />
(チャットでURLを送りあう)</p>
<p>今回作ったPHPスクリプトは100行程度のとても簡単なものなので、<br />
ぜひ改造して自分に使いやすい仕組みを作ってみてください！</p>
<p>ご要望やご質問、感想などがあれば私のtwitterまでどうぞ！<br />
(<a href="http://twitter.com/msk_masaki" target="_blank">http://twitter.com/msk_masaki</a>)</p>
<hr /><strong>[告知]</strong><br />
EC studioで本を出版することになりました！<br />
EC studioでiPhoneとtwitterを全社導入した時の事例や、<br />
Google Appsの活用法などを詳しくまとめています。<br />
Google 辻野社長との対談も収録！<br />
<span style="color:red">※Amazon総合ランキング1位獲得しました！</span></p>
<p><a href="http://www.ecstudio.jp/book/" target="_blank"><img class="alignnone size-full wp-image-900" title="「iPhoneとツイッターで会社は儲かる」" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/5ef0445a624cea69f4048ca66858f874.jpg" alt="「iPhoneとツイッターで会社は儲かる」" width="185" height="300" /></a></p>
<p><a href="http://www.ecstudio.jp/book/" target="_blank">詳しくはこちらの詳細ページをご覧下さい！</a></p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/tech-tips/autotweetcollector.html" title="twitterの興味のあるつぶやき(ツイート)を自動で収集する仕組みを作ってみました (2009/12/08 火曜日)">twitterの興味のあるつぶやき(ツイート)を自動で収集する仕組みを作ってみました</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/ultraquick-capture-for-twitter.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Chrome から好きな検索エンジンで検索する方法</title>
		<link>http://techblog.ecstudio.jp/tips/useful_search_by_google-chrome.html</link>
		<comments>http://techblog.ecstudio.jp/tips/useful_search_by_google-chrome.html#comments</comments>
		<pubDate>Sun, 21 Feb 2010 15:00:23 +0000</pubDate>
		<dc:creator>渋谷 悠司</dc:creator>
				<category><![CDATA[その他]]></category>
		<category><![CDATA[google chrome]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=686</guid>
		<description><![CDATA[みなさんはGoogle Chromeを使っていますか？ 最近は拡張機能も正式リリースされて、どんどん便利になっていきますね。 今回はそんなChromeの便利な使い方を一つご紹介します。 Chromeのアドレスバーは検索窓 [...]]]></description>
			<content:encoded><![CDATA[<p>みなさんは<a title="Google Chrome ダウンロード" href="http://www.google.com/chrome/intl/ja/landing.html?hl=ja&amp;hl=ja" target="_blank">Google Chrome</a>を使っていますか？<br />
最近は拡張機能も正式リリースされて、どんどん便利になっていきますね。</p>
<p>今回はそんなChromeの便利な使い方を一つご紹介します。</p>
<p>Chromeのアドレスバーは検索窓と兼用になっているんですが、<br />
ある設定を追加すると、<br />
任意の検索エンジンや、検索窓のあるサイトの検索をChromeからできてしまうんです。</p>
<p>たとえば・・・</p>
<p>YouTubeの検索をChromeからダイレクトにおこなう場合は</p>
<p>アドレスバーに「youtube」と入力して<br />
<img class="alignnone size-full wp-image-688" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204input_youtube.png" alt="100204input_youtube" width="257" height="71" /></p>
<p>スペース＋検索キーワードを入力すると、youtube検索というものが出現<br />
<img class="alignnone size-full wp-image-692" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204chage1.png" alt="100204chage" width="315" height="72" /></p>
<p>そして、検索<br />
<img src="../wp-content/uploads/2010/02/100204search_youtube2.png" alt="100204search_youtube" width="534" height="269" /></p>
<p>このように、Chromeから検索できました。</p>
<p>では、実際にどうやって設定を追加するのかをYouTubeを例に説明しようと思います。</p>
<p><span id="more-686"></span></p>
<h4>好きな検索エンジンの追加手順</h4>
<p>まずはChromeの右上のメニューからオプションを開きます</p>
<p><img class="alignnone size-full wp-image-704" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204select_option.png" alt="100204select_option" width="444" height="542" /></p>
<div style="margin-top:57px;margin-bottom:-26px">オプションを開いたら、<br />
基本設定タブの中の規定の検索エンジンの管理というボタンをクリック</div>
<p><img class="alignnone size-full wp-image-705" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204click_button.png" alt="100204click_button" width="580" height="612" /></p>
<p>ウィンドウが開いたら、右上の追加ボタンをクリックしましょう。<br />
すると、検索エンジンを追加という小さいウィンドウが開きます</p>
<p><img class="alignnone size-full wp-image-708" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204add_setting.png" alt="100204add_setting" width="579" height="454" /></p>
<p>ここまで来たら、検索エンジンの追加に必要なURLを作成します</p>
<p>まず、YouTubeで適当なキーワードで検索して、<br />
その際に表示されるURLをコピーしましょう。</p>
<p>例えばキーワードが「chrome」の場合</p>
<p>http://www.youtube.com/results?search_query=<span style="color: #0000ff">chrome</span>&amp;search_type=&amp;aq=f</p>
<p>URL中の「<span style="color: #0000ff">chrome</span>」というところを「<span style="color: #ff0000">%s</span>」に置き換えます　↓↓↓↓</p>
<p>http://www.youtube.com/results?search_query=<span style="color: #ff0000">%s</span>&amp;search_type=&amp;aq=f<br />
というURLができました。</p>
<p>このURLを、先程の検索エンジンの追加ウィンドウのURL欄へ入力します。</p>
<p><img class="alignnone size-full wp-image-714" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204add_engine.png" alt="100204add_engine" width="402" height="241" /></p>
<p>名前はYouTubeを検索するということがわかれば何でもOKです。<br />
ここでは「YouTube」にしておきます。</p>
<p>キーワードはショートカット名みたいなもののようなので、<br />
YouTubeで検索するさいに便利そうな文字にしておくのがいいと思います。</p>
<p>OKボタンを押すと、その他の検索エンジンのところにYouTubeが追加されました！<img class="alignnone size-full wp-image-722" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/100204added_engine.png" alt="100204added_engine" width="548" height="428" /></p>
<p>これで設定は終わりです。</p>
<p>結構簡単に追加できるので、よく使うところを追加しておくと便利だと思います。</p>
<p>例えば、PHPで開発している人なら<a href="http://www.php.net/" target="_blank">PHP.net</a>を検索できるようにするのも便利ですし、<br />
Gmailをよく利用する人はGmailの検索を追加してしまうのもいいかもしれません。</p>
<p>是非便利そうなところを追加してみてください＾＾</p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li>関連する投稿はありません。</li>
	</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tips/useful_search_by_google-chrome.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Amazon APIを使って簡単にランキングを取得する</title>
		<link>http://techblog.ecstudio.jp/tech-tips/amazon-api-ranking.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/amazon-api-ranking.html#comments</comments>
		<pubDate>Wed, 17 Feb 2010 00:00:17 +0000</pubDate>
		<dc:creator>小野</dc:creator>
				<category><![CDATA[PHP Tips]]></category>
		<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[開発環境 Tips]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=652</guid>
		<description><![CDATA[「iPhoneとツイッターで会社は儲かる」がAmazonで販売開始され、 EC studioでは様々なプロモーション活動を行っております。 プロモーションの効果測定をするために、Amazonの商品ページへアクセスして A [...]]]></description>
			<content:encoded><![CDATA[<p>「<a href="http://www.ecstudio.jp/book/" target="_blank">iPhoneとツイッターで会社は儲かる</a>」がAmazonで販売開始され、<br />
EC studioでは様々なプロモーション活動を行っております。</p>
<p>プロモーションの効果測定をするために、Amazonの商品ページへアクセスして<br />
Amazonの中で現在何位なのか順位チェックを行っていましたが、<br />
面倒くさくなったので自動ランキング取得システムを作成しました。</p>
<p>今回はAmazon APIを使って、簡単にAmazonの商品情報（ランキング）を<br />
取得する方法をご紹介いたします。</p>
<h5>今回の完成品</h5>
<p>こんな感じでAmazonの商品情報を取得できます。<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-71.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-71.png" alt="プログラム実行結果" title="プログラム実行結果" width="444" height="345" class="alignnone size-full wp-image-856" /></a><br />
[ 使用サービス ]　※無料<br />
・Amazon API<br />
・<a href="http://pear.php.net/manual/ja/" target="_blank">PEAR</a>の<a href="http://pear.php.net/package/Services_Amazon" target="_blank">Services_Amazon</a>ライブラリ</p>
<p>※Amazon API とは<br />
AmazonアソシエイトWebサービスが提供する「<a href="https://affiliate.amazon.co.jp/gp/advertising/api/detail/main.html" target="_blank">Product Advertising API</a>」のこと<br />
Amazonの商品情報や関連コンテンツをプログラムを通してアクセスできる<br />
サービスが提供されています。</p>
<p>※PEARのServices_Amazonライブラリとは<br />
PEARが提供するAmazon APIを簡単に使えるライブラリ</p>
<p><span id="more-652"></span></p>
<h4>サービス開始準備</h4>
<p>APIによるAmazonへのアクセスは、<a href="https://affiliate.amazon.co.jp/gp/associates/help/t126/a4?ie=UTF8&amp;pf_rd%5Ft=501&amp;ref%5F=amb%5Flink%5F84042136%5F4&amp;pf%5Frd%5Fm=AN1VRQENFRJN5&amp;pf%5Frd%5Fp=&amp;pf%5Frd%5Fs=center-1&amp;pf%5Frd%5Fr=&amp;pf%5Frd%5Fi=assoc%5Fhelp%5Ft126%5Fa1" target="_blank">利用者毎のアクセスキー</a>を使用した<a href="https://affiliate.amazon.co.jp/gp/associates/help/t126/a2?ie=UTF8&amp;pf_rd%5Ft=501&amp;ref%5F=amb%5Flink%5F84042136%5F2&amp;pf%5Frd%5Fm=AN1VRQENFRJN5&amp;pf%5Frd%5Fp=&amp;pf%5Frd%5Fs=center-1&amp;pf%5Frd%5Fr=&amp;pf%5Frd%5Fi=assoc%5Fhelp%5Ft126%5Fa1" target="_blank">署名認証</a>が<br />
必要になります。<br />
まずはアクセスキー（「Access Key ID」と「Secret Access Key」）を取得します。</p>
<p>1.Amazon Webサービスにログイン<br />
<a href="http://aws.amazon.com/" target="_blank">http://aws.amazon.com/</a></p>
<p>※アカウントがない場合は以下を参考にアカウントを作成してください。<br />
<a href="https://affiliate.amazon.co.jp/gp/associates/help/t125?ie=UTF8&amp;pf_rd%5Ft=501&amp;ref%5F=amb%5Flink%5F84045936%5F3&amp;pf%5Frd%5Fm=AN1VRQENFRJN5&amp;pf%5Frd%5Fp=&amp;pf%5Frd%5Fs=center-1&amp;pf%5Frd%5Fr=&amp;pf%5Frd%5Fi=assoc%5Fhelp%5Ft126%5Fa3" target="_blank">Product Advertising API アカウント作成 ヘルプ</a></p>
<p>2.ログイン後、「Account」メニューより、「Security Credentials」を選択</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-12.png" target="_blank" rel="lightbox"><img class="size-medium wp-image-670 alignnone" title="Amazon Webサービス - Security Credentials" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-12-300x147.png" alt="Amazon Webサービス - Security Credentials" width="300" height="147" /></a></p>
<p>3.「<a href="https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;action=access-key#access_credentials" target="_blank">Access Credentials</a>」に記載されている<br />
「Access Key ID」と「Secret Access Key」を取得<br />
※「Show」をクリックすると「Secret Access Key」が表示されます</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-21.png" target="_blank" rel="lightbox"><img class="size-medium wp-image-673 alignnone" title="Amazon 「Access Key ID」と「Secret Access Key」" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-21-300x150.png" alt="Amazon 「Access Key ID」と「Secret Access Key」" width="300" height="150" /></a></p>
<h4>商品識別コードの取得</h4>
<p>Amazonの商品情報を取得するのに、商品識別コードを使用します。<br />
本の場合は、ISBNの10桁コード（ISBN-10）を使用します。<br />
※本以外の商品の場合はASINコードを使用します</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-62.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-62.png" alt="Amazon登録情報 商品識別コード（ISBN）" title="2010-02-04-6" width="245" height="235" class="size-full wp-image-812" /></a><br />
<em>※Amazon商品ページより抜粋</em></p>
<h4>Services_Amazonをインストール</h4>
<p>PEARの<a href="http://pear.php.net/package/Services_Amazon" target="_blank">Services_Amazon</a>のライブラリを使ってAmazonにアクセスし、<br />
ランキング情報を取得します。<br />
※PEARとはPHPの標準のオープンソースのクラスライブラリです。</p>
<h5>Windowsのインストール方法</h5>
<p>インストールは以下のコマンドを実行します<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-05-15-21-041.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-05-15-21-041.png" alt="PEAR インストールコマンド" title="PEAR インストールコマンド" width="350" height="109" class="alignnone size-full wp-image-832" /></a><br />
※PEARのServices_Amazonの<a href="http://pear.php.net/package/Services_Amazon/download" target="_blank">Downloadページ</a>より</p>
<h5>ローカル環境（xampp）にインストール</h5>
<p>今回は、ローカル環境（xampp）にインストールしてみます。<br />
※xamppとは<a href="http://www.apachefriends.org/jp/xampp-windows.html" target="_blank">ApacheFriends</a>が提供しているフリーソフトで、<br />
PHPの開発環境を簡単にインストールして使うことができます。<br />
※Services_Amazonは標準で用意されていないので、手動でインストール。</p>
<p>1.コマンドプロンプトを立ち上げる<br />
Windows > すべてのプログラム > アクセサリ > コマンド プロンプト</p>
<p>2.ディレクトリ変更<br />
&gt;cd C:\xampp\php<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-57-01.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-57-01.png" alt="xampp ディレクトリ変更" title="xampp ディレクトリ変更" width="262" height="128" class="alignnone size-full wp-image-1050" /></a></p>
<p>※パスはご自分のxamppがインストールしてある環境にあわせてください</p>
<p>3.PEAR <a href="http://pear.php.net/package/HTTP_Request2/download" target="_blank">HTTP_Request2</a>のインストール<br />
&gt;pear install -a HTTP_Request2-alpha<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-30-45.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-30-45.png" alt="PEAR HTTP_Request2のインストール" title="PEAR HTTP_Request2のインストール" width="520" height="282" class="alignnone size-full wp-image-1044" /></a></p>
<p>※Services_Amazonで使用しているHTTP_Request内で使用しているので、<br />
まずこちらをインストール。</p>
<p>※インストールのバージョン（state）は最新のものを指定してください</p>
<p>※インストールする際に、依存するパッケージも合わせてインストールするため、<br />
オプションに「-a」を指定しています。</p>
<p>4.PEAR Services_Amazon のインストール<br />
&gt;pear install -a Services_Amazon-beta<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-40-40-e1279872211515.png" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-40-40-e1279872211515.png" alt="PEAR Services_Amazonのインストール" title="PEAR Services_Amazonのインストール" width="551" height="405" class="alignnone size-full wp-image-1046" /></a></p>
<p>※インストールのバージョン（state）は最新のものを指定してください</p>
<p>4.インストールが終わったら確認（インストール・パッケージ一覧）<br />
&gt;pear list<br />
<a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-43-41.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-17-43-41.png" alt="PEAR インストール・パッケージ一覧" title="PEAR インストール・パッケージ一覧" width="364" height="421" class="alignnone size-full wp-image-1048" /></a></p>
<p>※「Services_Amazon」最新版（2010/02/16時点で0.8.0）が<br />
インストールされていることを確認</p>
<h5>その他の環境や方法でのインストール</h5>
<p>PEAR公式サイトの<a href="http://pear.php.net/manual/ja/installation.php" target="_blank">インストールマニュアル</a>を御覧下さい。</p>
<h4>ItemLookupを使用してAmazon商品情報を取得</h4>
<h5>サンプルコード</h5>
<p>PEARのServices_Amazonを使用してPHPでAmazonから商品情報を<br />
取得して、結果を表示するプログラム</p>
<div class="igBar"><span id="lphp-70"><a href="#" onclick="javascript:showCodeTxt('php-70'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-70">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;!DOCTYPE html PUBLIC <span style="color:#ff00ff;">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color:#ff00ff;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html xmlns=<span style="color:#ff00ff;">"http://www.w3.org/1999/xhtml"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;meta http-equiv=<span style="color:#ff00ff;">"Content-Type"</span> content=<span style="color:#ff00ff;">"text/html; charset=UTF-8"</span> /&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;title&gt;Amazon商品ランキング&lt;/title&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">&lt;?php</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//アクセスキー設定</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$access_key_id</span> = <span style="color:#ff00ff;">'XXXXXXXXXXXXXXXXX'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$secret_access_key</span> = <span style="color:#ff00ff;">'XXXXXXXXXXXXXXXXXXXXXX'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//Amazonサービス情報取得</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">include_once</span> <span style="color:#ff00ff;">'Services/Amazon.php'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$amazon</span> = <span style="color:#800000;">new</span> Services_Amazon<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$access_key_id</span>,<span style="color:#663333;">$secret_access_key</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$amazon</span>-&gt;<span style="color:#006600;">setLocale</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'JP'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//商品情報取得</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$options</span> = <span style="color:#0000ff;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff00ff;">'ResponseGroup'</span> =&gt; <span style="color:#ff00ff;">'ItemAttributes,SalesRank,Images'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$result</span> = <span style="color:#663333;">$amazon</span>-&gt;<span style="color:#006600;">ItemLookup</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'4839934444'</span>,<span style="color:#663333;">$options</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!PEAR::<span style="color:#006600;">isError</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$result</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000;">//商品情報表示</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#ff00ff;">"&lt;img src='"</span>.<span style="color:#663333;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Item'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'MediumImage'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'URL'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#ff00ff;">"'&gt;&lt;br /&gt;"</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#663333;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Item'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'ItemAttributes'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Title'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#ff00ff;">'&lt;br /&gt;'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#663333;">$result</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Item'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'SalesRank'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#ff00ff;">' 位 ('</span>.<a target="_blank"&nbsp; href="http://www.php.net/date"><span style="color:#ff0000;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'Y/m/d H:i:s'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">')'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#0000ff;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000;">//エラー処理</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$error_message</span> = <span style="color:#ff00ff;">'Amazonの情報取得に失敗しました'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$error</span> = <span style="color:#663333;">$amazon</span>-&gt;<span style="color:#006600;">getError</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$error_message</span> .= <span style="color:#ff00ff;">'（'</span>.<span style="color:#663333;">$error</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Message'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#ff00ff;">'）'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$error_message</span> .= <span style="color:#ff00ff;">'【Code:'</span>.<span style="color:#663333;">$error</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'Code'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#ff00ff;">'】'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#0000ff;">else</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$error_message</span> .= <span style="color:#ff00ff;">'（'</span>.<span style="color:#663333;">$result</span>-&gt;<span style="color:#006600;">getMessage</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">'）'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#663333;">$error_message</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">?&gt;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>・アクセスキー（10行目,11行目）は「サービス開始準備」で取得したアクセスキーを指定してください。</p>
<p>・Amazonの何の商品情報を取得するかはItemLookupの第二引数のResponseGroupで指定します。</p>
<p>※Services_AmazonのItemLookupに関するドキュメント（英語）は<a href="http://pear.php.net/package/Services_Amazon/docs/latest/Services_Amazon/Services_Amazon.html#methodItemLookup" target="_blank">こちら</a></p>
<p>PEAR::Services_Amazonを用いると取得した情報等はXMLではなく<br />
連想配列で返されるのでPHPから操作するにはとても便利です。</p>
<h5>実行結果</h5>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-71.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-04-71.png" alt="プログラム実行結果" title="プログラム実行結果" width="444" height="345" class="alignnone size-full wp-image-856" /></a></p>
<h4>活用サンプル例（おまけ）</h4>
<p>弊社では、プログラムを定期実行して、取得したランキングを保存して<br />
色々な形で簡単にAmazonランキングを把握できるようにしています。</p>
<h5>レポートで推移を表示</h5>
<p>レポートにすることで、過去のランキング変動がすぐに分かります。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-05-16-28-46.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-05-16-28-46.png" alt="Amazonランキングレポート" title="Amazonランキングレポート" width="558" height="261" class="alignnone size-full wp-image-842" /></a></p>
<h5>Twitterで自動的につぶやくbot</h5>
<p>現在のランキングをすぐに把握することが出来ます。<br />
タイムラインに流れてくるので、操作をするのさえ面倒くさい人にはおすすめ。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-20-36-271.png" target="_blank" rel="lightbox"><img src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/2010-02-16-20-36-271.png" alt="2010-02-16 20-36-27" title="2010-02-16 20-36-27" width="548" height="255" class="alignnone size-full wp-image-1075" /></a><br />
※Amazonランキングつぶやきbotは<a href="http://twitter.com/ec_rank_bot" target="_blank">こちら</a></p>
<p>とても簡単に、Amazonのリアルタイムの商品情報を取得でき、<br />
活用することができるので、ぜひ試してみてください。</p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/news/vol7.html" title="週間TechNews vol.7 &#8211; Photoshop Express,AIR Linux版,Amazon FWS (2008/04/06 日曜日)">週間TechNews vol.7 &#8211; Photoshop Express,AIR Linux版,Amazon FWS</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech/newhpplus-hiring.html" title="第13回合宿スタート！ &#8211; サーバーエンジニアを募集中 (2007/11/29 木曜日)">第13回合宿スタート！ &#8211; サーバーエンジニアを募集中</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/re-phpeclipse.html" title="環境一新:やっぱりPDTじゃなくPHPEclipseにした理由 (2008/07/16 水曜日)">環境一新:やっぱりPDTじゃなくPHPEclipseにした理由</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/php/phpeclipse.html" title="テキストエディタからPHPEclipseへ移行した決め手 (2007/06/20 水曜日)">テキストエディタからPHPEclipseへ移行した決め手</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech/ganbaru-php.html" title="「ガンバルPHPの会」はじめました (2008/06/23 月曜日)">「ガンバルPHPの会」はじめました</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/amazon-api-ranking.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google App Engineを使って無料でサイトを立ち上げる方法</title>
		<link>http://techblog.ecstudio.jp/tech-tips/freewebsite-with-google-app-engine.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/freewebsite-with-google-app-engine.html#comments</comments>
		<pubDate>Sun, 07 Feb 2010 14:45:36 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[開発環境 Tips]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[クラウド]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=858</guid>
		<description><![CDATA[Googleが提供する、Google App Engineというサービスを知っていますか？ Amazon EC2などと同じで、Googleが用意するクラウドサーバー環境で アプリケーション開発ができるというサービスです。 [...]]]></description>
			<content:encoded><![CDATA[<p>Googleが提供する、Google App Engineというサービスを知っていますか？</p>
<p><img class="alignnone size-full wp-image-859" title="Google App Engine" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/appengine_lowres.gif" alt="Google App Engine" width="142" height="109" /></p>
<p>Amazon EC2などと同じで、Googleが用意するクラウドサーバー環境で<br />
アプリケーション開発ができるというサービスです。<br />
(レンタルサーバーのようなもの)</p>
<p>その大きな特徴は、なんといっても月間500万PV相当まで<strong>"無料"</strong>ということです。<br />
※有料で制限を拡張することも可能</p>
<p><span style="color:red">※2011/09/07 注<br />
Google App Engineの<a href="http://www.google.com/enterprise/cloud/appengine/pricing.html" target="_blank">新料金体系</a>が発表されました。<br />
新料金体系では無料で使える枠が大幅に削減されています。<br />
この記事の無料での使用制限に関する記述は、新料金体系では<br />
正しくありませんのでご注意ください。<br />
</span></p>
<p>「App Engine は無料で始めることができます。最大 500 MB の永続性ストレージに加え、月間約 500 万ページ ビューに対応できる十分な CPU と帯域幅を、すべてのアプリケーションで完全に無料で利用できます。」<br />
- <a href="http://code.google.com/intl/ja/appengine/docs/whatisgoogleappengine.html" target="_blank">Google App Engine公式ドキュメントより</a></p>
<p>月間500万PVというと、相当な人気サイトでも余裕でさばけるようなスペックです。<br />
これが無料というのはスゴイ・・・。</p>
<p>もちろんGoogleが提供する環境なので、信頼性の高さ(落ちにくさ)は間違いなしです。</p>
<p>このGoogle App Engine(以下<strong> GAE</strong>)、アプリケーション開発だけでしか<br />
使えないと思われがちなのですが、実は設定を工夫すれば<br />
通常のHTMLによるサイトを作って運用することも可能です。</p>
<p>多少初期設定の手順は複雑ですが、このスペックのサーバーを無料で<br />
使用出来ることを考えれば試してみる価値はあるのではないかと思います。</p>
<p>Webサイトを立ち上げるまでの手順をまとめてみましたので、公開したいと思います。<br />
※縮小してある画像はクリックして拡大できます</p>
<p><span id="more-858"></span></p>
<h4>1.Google App Engineを登録する</h4>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_top.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-862" title="Google App Engine 登録" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_top-300x134.png" alt="Google App Engine 登録" width="300" height="134" /></a></p>
<p>まずは、Google App Engineに登録する必要があります。<br />
Googleアカウント(GMAIL)が必要なので、<br />
持っていない方は事前に作成してください。</p>
<p>下記のURLから登録できます。</p>
<p><a href="https://appengine.google.com/" target="_blank">https://appengine.google.com/</a></p>
<p>※<a href="http://www.start-apps.jp/" target="_blank">Google Apps</a>でも利用可能です。その場合は、</p>
<p>https://appengine.google.com/a/ドメイン名</p>
<p>から登録してください。</p>
<p>アカウントの登録ができたら、早速アプリケーションを作りましょう。<br />
GAEにログインすると下記のような画面になります。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_create_app.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-863" title="アプリケーションの作成" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_create_app-300x108.png" alt="アプリケーションの作成" width="300" height="108" /></a></p>
<p>「Create Application」 をクリックします。<br />
初回だけ、携帯メールでの認証が必要になります。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_verify_sms.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-864" title="携帯認証" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_verify_sms-300x174.png" alt="携帯認証" width="300" height="174" /></a></p>
<p>SMSと書いてますが、通常のメールアドレスで大丈夫です。</p>
<p>CountryにJapan、キャリアにdocomoなどを設定して、<br />
Usernameのところにはメールアドレスを入力しましょう。</p>
<p>設定すると、携帯にメールが飛んできますので、<br />
そのメール内にあるGoogle App Engine Codeを<br />
フォームに入力すれば完了です。</p>
<p>認証が完了すれば、アプリケーションを作成できます。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_create_app_form.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-896" title="アプリケーション作成フォーム" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_create_app_form-300x290.png" alt="アプリケーション作成フォーム" width="300" height="290" /></a></p>
<p>作成するアプリケーション(サイト)の情報を入力しましょう。<br />
「Application Identifier」がアプリケーション一つ一つに割り当てられるサブドメイン名になります。<br />
この部分は<strong>後から変更したり、削除することができない</strong>ので注意！<br />
ドメインを取得するつもりで設定しましょう。</p>
<p>タイトルは後からでも変更できます。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_myapplications1.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-983" title="アプリケーション一覧" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_myapplications1-300x118.png" alt="アプリケーション一覧" width="300" height="118" /></a></p>
<p>作成が完了しました。<br />
いまのところ、最大10個まで無料でアプリケーションを作成できます。</p>
<p>これで、オンライン側の設定は完了です！</p>
<h4>2.開発環境のセットアップ</h4>
<p>GAEへファイルをアップロードするには、FTPソフトなどの代わりに<br />
Googleが用意する開発環境(SDK)を使用します。</p>
<p>いくつかソフトをインストールする必要があるので、<br />
下記の手順でインストールしてください。</p>
<p>1.Pythonをダウンロード (v2.6.4)</p>
<p>GAEで動くスクリプト環境のPythonをインスールします。<br />
各OSにあったファイルをダウンロードしてください。<br />
(Python 3.x.xではなく、2.x.xのバージョンを選んでください)</p>
<p><a href="http://www.python.org/download/" target="_blank">http://www.python.org/download/</a></p>
<p><img class="alignnone size-medium wp-image-868" title="Pythonのダウンロード" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/python_download-300x201.png" alt="Pythonのダウンロード" width="300" height="201" /></p>
<p>1.1 Pythonをインストール</p>
<p><img class="alignnone size-medium wp-image-869" title="Pythonのインストール" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/python_install-300x256.png" alt="Pythonのインストール" width="300" height="256" /></p>
<p>素直に「Next」を選んでいけばokです。</p>
<p>2.Google App Engine SDK をダウンロード</p>
<p>次にGAEの開発環境であるSDKをインストールします。</p>
<p><a href="http://code.google.com/intl/ja/appengine/downloads.html#Download_the_Google_App_Engine_SDK" target="_blank">http://code.google.com/intl/ja/appengine/downloads.html</a></p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_download.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-870" title="GAE SDKダウンロード" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_download-300x157.png" alt="GAE SDKダウンロード" width="300" height="157" /></a></p>
<p>2.1 Google App Engine SDK をインストール</p>
<p><img class="alignnone size-medium wp-image-871" title="GAE SDKインストール" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_install-300x232.png" alt="GAE SDKインストール" width="300" height="232" /></p>
<p>こちらも、そのまま「Next」を選べばokです。<br />
これでGAEを使った開発環境のセットアップが完了しました！</p>
<h4>3.Google App Engine Launcherを使ってファイルをアップロード</h4>
<p>ではさっそく、先程インストールしたGoogle App Engine SDKを起動しましょう。</p>
<p><img class="alignnone size-medium wp-image-872" title="GAEランチャーの起動" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_menu-182x300.png" alt="GAEランチャーの起動" width="182" height="300" /></p>
<p>スタートメニューから、<br />
「Google App Engine Launcher」を選択します。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-873" title="GAEランチャー" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher-300x211.png" alt="GAEランチャー" width="300" height="211" /></a></p>
<p>こんな画面が出てくればokです。<br />
このアプリケーションを使ってファイルのアップロードなどを行います。<br />
では、アプリケーションを追加してみましょう。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_create_app.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-874" title="GAEランチャー アプリケーション作成" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_create_app-300x191.png" alt="GAEランチャー アプリケーション作成" width="300" height="191" /></a></p>
<p>メニューの「File」から<br />
「Create New Application..」をクリックします。</p>
<p><img style="border: 0px initial initial;" title="GAEランチャー アプリケーションの作成" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_create_app_dialog.png" alt="GAEランチャー アプリケーションの作成" width="545" height="281" /></p>
<p>「Application Name」に、最初にブラウザ側で作成した<br />
アプリケーション名を入れます。<br />
(Application Identiferの部分。サブドメインにあたるところです)</p>
<p>「Parent Directory」にはhtmlファイルなどを<br />
入れるフォルダを指定します。<br />
(指定したフォルダに、アプリケーション名のサブフォルダが作成されます)</p>
<p>作成すると、下部のボックスに表示されるようになります。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_created_app_selected.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-879" title="GAEランチャー アプリケーション選択" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_created_app_selected-300x122.png" alt="GAEランチャー アプリケーション選択" width="300" height="122" /></a></p>
<p>作成したアプリケーションを選択し、「Run」ボタンをクリックします。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_run_app.png" target="_blank" rel="lightbox"><img class="alignnone size-medium wp-image-880" title="GARランチャー アプリケーション起動" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_run_app-300x98.png" alt="GARランチャー アプリケーション起動" width="300" height="98" /></a></p>
<p>するとアプリケーションが起動します。<br />
「Browse」ボタンを押すとローカル環境でテスト表示させることができます。<br />
サーバーへのアップロードには、「Deploy」ボタンを使用します。</p>
<p>「Deploy」を押して、サーバーへアップしてみましょう。</p>
<p><img class="alignnone size-medium wp-image-889" title="デプロイ認証" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_deploy1-300x169.png" alt="デプロイ認証" width="300" height="169" /></p>
<p>Googleの認証を求められるので、Google App Engineに登録した<br />
メールアドレスとパスワードを入力します。</p>
<p><img class="alignnone size-medium wp-image-883" title="GAEランチャー デプロイログ" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_launcher_deploy_log-300x144.png" alt="GAEランチャー デプロイログ" width="300" height="144" /></p>
<p>認証に成功すれば、ファイルがサーバーへと自動的にアップロードされます。</p>
<p>http://[アプリケーション名].appspot.com</p>
<p>をブラウザで表示してみてください。<br />
([アプリケーション名]の ところはApplication Identiferを入れてください)</p>
<p>Hello World!</p>
<p>と表示されていれば成功です！</p>
<h4>4.Google App Engineを使ってWebサイトを立ち上げる</h4>
<p>お待たせしました、ここからが本番です。<br />
さきほどの様な要領でファイルをサーバーにアップロードできるのですが、<br />
初期設定ではHTMLなどのファイルを上げても表示することができません。</p>
<p>アプリケーション開発用の設定になっているからなのですが、これを<br />
Webサイト用の設定に書き換える必要があります。</p>
<p>では、Google App Engine Launcherで作成したアプリケーションの、<br />
Parent Directoryで指定したフォルダの中にあるファイルをエクスプローラーで開いてください。</p>
<p><img class="alignnone size-full wp-image-884" title="デフォルトのファイル" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_default_files.png" alt="デフォルトのファイル" width="223" height="152" /></p>
<p>このような、3つのファイルがあるかと思います。<br />
初期状態ではリクエストはすべてmain.pyというファイルで<br />
処理されるような設定になっていますが、ここを変更して<br />
HTMLファイルなどを自由に表示できるようにしてみます。</p>
<p>まず、HTMLファイルなどを入れるフォルダを作成します。</p>
<p><img class="alignnone size-full wp-image-885" title="htdocsフォルダを作成" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_create_htdocs.png" alt="htdocsフォルダを作成" width="227" height="177" /></p>
<p>ここではサーバーの慣習にならって「htdocs」というフォルダを作りました。<br />
（別のフォルダ名でも全然okです)</p>
<p>このフォルダの中に、アップしたいサイトのHTMLファイルや画像、<br />
CSSファイルなどを入れておきます。</p>
<p><img class="alignnone size-full wp-image-886" title="サイトのファイルを入れておく" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/gae_site_files.png" alt="サイトのファイルを入れておく" width="178" height="147" /><br />
次に、最初からある「app.yaml」 というファイルを編集します。<br />
(メモ帳などのテキストエディタで開きます)</p>
<p>初期設定では</p>
<div class="igBar"><span id="lcode-73"><a href="#" onclick="javascript:showCodeTxt('code-73'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-73">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">application: <span style="color:#006600; font-weight:bold;">&#91;</span>xxxxxx<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">version: <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">runtime: python</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">api_version: <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">handlers:</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">- url: .*</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">script: main.<span style="">py</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>※[xxxxxx]の部分は、各アプリケーション名<br />
こうなっていますが、これを</p>
<div class="igBar"><span id="lcode-74"><a href="#" onclick="javascript:showCodeTxt('code-74'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="code-74">
<div class="code">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">application: <span style="color:#006600; font-weight:bold;">&#91;</span>xxxxxx<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">version: <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">runtime: python</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">api_version: <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">handlers:</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">- url: /</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">static_files: htdocs/index.<span style="">html</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">upload: /</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">- url: /</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">static_dir: htdocs </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>この様に変更します。<br />
(handlers: 以降の行を変更)</p>
<p>こう設定することで、htdocs/ 以下のフォルダを<br />
Webサイトとして表示することができるようになりました。</p>
<p>例えば、</p>
<p>http://[アプリケーション名].appspot.com/page.html</p>
<p>↓<br />
htdocs/page.html</p>
<p>と関連付けられ、サブフォルダがある場合でも</p>
<p>http://[アプリケーション名].appspot.com/image/logo.jpg</p>
<p>↓<br />
htdocs/image/logo.jpg</p>
<p>と、同様に関連付けられるようになります。<br />
アップしたいファイルを入れて、「Deploy」してみましょう！</p>
<h4>完成！</h4>
<p>お疲れ様でした！</p>
<p>あとはサイトの更新時にはhtdocsフォルダの中身を変更し、<br />
随時Deployすればサーバーへアップロードできます。</p>
<p>このGoogle App Engineには<a href="http://www.google.com/support/a/bin/answer.py?hl=jp&amp;answer=91077" target="_blank">独自ドメインを使用することもできる</a>ので、<br />
本当にレンタルサーバー的に使用することができます。</p>
<p>GAEの詳しい使い方や、設定の詳細を知りたい方は<br />
Googleのドキュメントを参照してください。</p>
<p>デベロッパー ガイド<br />
<a href="http://code.google.com/intl/ja/appengine/docs/" target="_blank"> http://code.google.com/intl/ja/appengine/docs/</a></p>
<p>ただ、GAEの欠点としてはPHPなどのプログラムを動かすことができません。<br />
PythonやJavaのアプリケーションは動作しますが、<br />
Googleの特殊な環境で動作するようになっていないといけません。</p>
<p>単純なお問い合わせフォームや注文フォームなどだけの場合は、<br />
Googleドキュメントのフォーム機能を使うことをオススメします。<br />
(とても簡単にお問い合わせフォームなどを組み込めます)</p>
<p>参考リンク：<a href="http://google-mania.net/archives/1039" target="_blank">http://google-mania.net/archives/1039</a></p>
<p>またGAEを使ってみて面白いことがわかればブログ記事にしていきたいと思います。 <img src='http://techblog.ecstudio.jp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>ご要望やご質問、感想などがあれば私のtwitterまでどうぞ！<br />
(<a href="http://twitter.com/msk_masaki" target="_blank">http://twitter.com/msk_masaki</a>)</p>
<hr />
<p><strong>[告知]</strong><br />
EC studioで本を出版することになりました！<br />
EC studioでiPhoneとtwitterを全社導入した時の事例や、<br />
Google Appsの活用法などを詳しくまとめています。<br />
Google 辻野社長との対談も収録！</p>
<p><a href="http://www.ecstudio.jp/book/" target="_blank"><img class="alignnone size-full wp-image-900" title="「iPhoneとツイッターで会社は儲かる」" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/5ef0445a624cea69f4048ca66858f874.jpg" alt="「iPhoneとツイッターで会社は儲かる」" width="185" height="300" /></a></p>
<p><a href="http://www.ecstudio.jp/book/" target="_blank">詳しくはこちらの詳細ページをご覧下さい！</a></p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/news/vol8.html" title="週間TechNews vol.8 &#8211; Google App Engine,Adobe Media Player (2008/04/11 金曜日)">週間TechNews vol.8 &#8211; Google App Engine,Adobe Media Player</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/vol7.html" title="週間TechNews vol.7 &#8211; Photoshop Express,AIR Linux版,Amazon FWS (2008/04/06 日曜日)">週間TechNews vol.7 &#8211; Photoshop Express,AIR Linux版,Amazon FWS</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/vol6.html" title="週間TechNews vol.6 &#8211; Google Sites,AIR 正式版リリース (2008/02/29 金曜日)">週間TechNews vol.6 &#8211; Google Sites,AIR 正式版リリース</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/vol4.html" title="週間TechNews vol.4 &#8211; Fastladderオープンソース化,OpenIDにGoogle/MS/Yahoo参加、はてな京都へ (2008/02/15 金曜日)">週間TechNews vol.4 &#8211; Fastladderオープンソース化,OpenIDにGoogle/MS/Yahoo参加、はてな京都へ</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/weekly/vol3.html" title="週間TechNews vol.3 &#8211; Zend Framework 1.5,Gmail 絵文字対応,Google Docs同期 (2008/01/31 木曜日)">週間TechNews vol.3 &#8211; Zend Framework 1.5,Gmail 絵文字対応,Google Docs同期</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/freewebsite-with-google-app-engine.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Eclipse3.5PDT＋Zend Debugger＋XAMPPでラクラクPHPデバッグ</title>
		<link>http://techblog.ecstudio.jp/tech-tips/devenv-tips/zend-debugger.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/devenv-tips/zend-debugger.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 01:50:45 +0000</pubDate>
		<dc:creator>池田 雅春</dc:creator>
				<category><![CDATA[開発環境 Tips]]></category>
		<category><![CDATA[デバッグ]]></category>
		<category><![CDATA[開発環境]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=589</guid>
		<description><![CDATA[PHPでデバッグする際に、「echo」や「print_r」を大量に書いていたりしませんか？ そんな方達のために、EclipseとZend Debuggerを使った簡単デバッグ方法について説明したいと思います。 [前提条件 [...]]]></description>
			<content:encoded><![CDATA[<p>PHPでデバッグする際に、「echo」や「print_r」を大量に書いていたりしませんか？<br />
そんな方達のために、EclipseとZend Debuggerを使った簡単デバッグ方法について説明したいと思います。</p>
<p>[前提条件]<br />
OS : Windows<br />
EClipse3.5(PDT)日本語化パッチ適用、およびXAMPPをインストール済み</p>
<h4>Zend Debuggerの設定</h4>
<p>(1) <a href="http://downloads.zend.com/pdt/server-debugger/" target="_blank">公式サイト</a>から「ZendDebugger-x.x.x-cygwin_nt-i386.zip」をダウンロードし、適当なディレクトリに展開する<br />
※「x」の部分はバージョン</p>
<p>(2)「xampp\php」以下に「ZendDebugger」フォルダを作成し、その中に先ほど解凍したフォルダ内の「x_x_x_comp\ZendDebugger.dll」をコピーする<br />
※「x」の部分はバージョン。PHP5.2環境の場合は「5_2_x_comp」フォルダからコピーする。</p>
<p>(3)「xampp\apache\bin\php.ini」を編集する</p>
<p>[Zend]<br />
zend_extension_ts= "C:\xampp\php\ZendDebugger\ZendDebugger.dll"<br />
zend_debugger.allow_hosts= 127.0.0.1/192.168.0.*<br />
zend_debugger.deny_hosts = all<br />
zend_debugger.expose_remotely = always</p>
<p>※ディレクトリパスやIPは状況に応じて変更してください。<br />
※もともと[Zend]内に記述されていた項目は状況に応じてコメントアウトしてください</p>
<p>(4)インストール確認<br />
phpinfo()で「Zend Debugger」という文字列が表示されている事を確認</p>
<p><img class="alignnone size-full wp-image-644" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/phpinfo.png" alt="phpinfo" width="558" height="270" /></p>
<p><span id="more-589"></span></p>
<h4>Eclipseでの実行手順</h4>
<p>1.「実行」→「デバッグの構成」を選択し、「PHPWebページ」から新しいデバッグの設定を行う</p>
<p><img class="alignnone size-full wp-image-598" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/01/new_debug.png" alt="new_debug" width="206" height="296" /></p>
<p>2.デバッグの詳細な設定を行う</p>
<p><img class="alignnone size-full wp-image-603" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/01/debug_detail.png" alt="debug_detail" width="556" height="408" /></p>
<p>名前 ： 任意の名称<br />
サーバー・デバッガー ： Zendデバッガー<br />
PHPサーバー ： Default PHP Server<br />
ファイル ： 実行ファイルのパスを入力</p>
<p>2.「デバッグ」ボタンをクリックし、ソースの最初の行にカーソルが合うことを確認する</p>
<p><img class="alignnone size-full wp-image-613" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/debug_start.png" alt="debug_start" width="383" height="284" /></p>
<p>※ブレークポイントは行数の左側をダブルクリックすると作成できます</p>
<p>4.ステップ実行する<br />
[デバッグウインドウ]<br />
緑矢印　：　次のブレークポイントまで進む<br />
赤四角　：　デバッグを中止する</p>
<p><img class="alignnone size-full wp-image-614" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/debug_button.png" alt="debug_button" width="384" height="191" /></p>
<p>[ファンクションキー]<br />
F5　：　メソッドの中へ進む<br />
F6　：　１行進む<br />
F8　：　次のブレークポイントまで進む</p>
<p>[変数ウインドウ]</p>
<p><img class="alignnone size-full wp-image-615" src="http://techblog.ecstudio.jp/wp-content/uploads/2010/02/debug_variable.png" alt="debug_variable" width="359" height="407" /><br />
変数の中身を確認できます</p>
<p>Zend Debuggerを使うと、処理のどの部分を通っているのか一目瞭然、<br />
echoなどのコマンドも不要なのでデバッグ効率も上がります。</p>
<p>設定も簡単なので一度試してみてはいかがでしょうか？</p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/tech-tips/php/phpeclipse.html" title="テキストエディタからPHPEclipseへ移行した決め手 (2007/06/20 水曜日)">テキストエディタからPHPEclipseへ移行した決め手</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/subversion-practice.html" title="SubVersionでチーム開発を加速させる2 &#8211; 実践編(TortoiseSVNチュートリアル) (2007/10/26 金曜日)">SubVersionでチーム開発を加速させる2 &#8211; 実践編(TortoiseSVNチュートリアル)</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/subversion-basics.html" title="SubVersionでチーム開発を加速させる &#8211; 基礎知識編 (2007/10/22 月曜日)">SubVersionでチーム開発を加速させる &#8211; 基礎知識編</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/smarty-outline.html" title="Smartyのテンプレートを見やすくアウトライン表示するハック (2008/07/24 木曜日)">Smartyのテンプレートを見やすくアウトライン表示するハック</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/php/php-devenv.html" title="PHPの開発環境は何にするべきなのか？ (2007/06/15 金曜日)">PHPの開発環境は何にするべきなのか？</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/devenv-tips/zend-debugger.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>twitterの興味のあるつぶやき(ツイート)を自動で収集する仕組みを作ってみました</title>
		<link>http://techblog.ecstudio.jp/tech-tips/autotweetcollector.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/autotweetcollector.html#comments</comments>
		<pubDate>Tue, 08 Dec 2009 06:55:47 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yahoo pipes]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=545</guid>
		<description><![CDATA[twitterを使っている時に、自分が興味のあるつぶやきが フォローや検索をしなくても勝手にタイムラインに 流れてきたらなぁ・・・と思ったことはありませんか？ 私もtwitterを使ってよくEC studio関連のことや [...]]]></description>
			<content:encoded><![CDATA[<p>twitterを使っている時に、自分が興味のあるつぶやきが<br />
フォローや検索をしなくても勝手にタイムラインに<br />
流れてきたらなぁ・・・と思ったことはありませんか？</p>
<p>私もtwitterを使ってよくEC studio関連のことや、<br />
新しく出した<a href="http://www.web-analyst.jp" target="_blank">アクセス解析サービス</a>についてのつぶやきが見たくて、<br />
毎回検索していましたが、面倒になったので<br />
自動で集めてくる仕組みを作ってみました！</p>
<h4>今回の完成品</h4>
<p>今回の完成品です。</p>
<p><img class="alignnone size-full wp-image-546" title="自動つぶやき収集ツール" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/autotweet.png" alt="自動つぶやき収集ツール" width="570" height="581" /></p>
<p>専用のアカウントを作成し、そのアカウントが<br />
興味のあるキーワードを含んだつぶやきをtwitter全体から検索し、<br />
見つけたら自動でつぶやくようにしてみました。<br />
<span id="more-545"></span></p>
<p>この画像の例で言うと、EC studioという文字が<br />
含まれるつぶやきがあれば自動でつぶやいてくれます。<br />
(ecstudioでも、ECスタジオでも、イーシースタジオでもok)</p>
<p>そのアカウントさえフォローしておけば、<br />
自分のタイムラインにどんどん流れてくるというワケです。</p>
<p>つぶやいた人のアカウント名がわかるように、<br />
先頭に(@アカウント名) で入るようにしています。</p>
<p>@付きなのでクリックすればその人をすぐに<br />
フォローすることもできます。</p>
<p><strong>※非公開アカウントにして、@付きのつぶやきでも<br />
相手の迷惑にならないようにしています。</strong></p>
<h4>仕組み</h4>
<p>使っているツールは下記の三つです。<br />
(全て無料です。感謝！)</p>
<p>・<a href="http://search.twitter.com/" target="_blank">Twitter Search</a><br />
・<a href="http://pipes.yahoo.com/pipes/" target="_blank">Yahoo! Pipes</a><br />
・<a href="http://twitterfeed.com/" target="_blank">twitterfeed</a></p>
<p>これらのツールを組み合わせて作ります。</p>
<p>仕組みとしては、Twitter Searchで<br />
興味のあるキーワードを検索したフィードを生成し、<br />
そのフィードをYahoo! Pipesで加工し、<br />
twitterfeedでフィードをつぶやかせています。</p>
<p>※Twitter Searchの検索フィードではつぶやいた人の<br />
アカウント名がわからないので、Yahoo! Pipesを使って<br />
フィードのタイトルにつぶやいた人のアカウント名が<br />
入るようにしています。</p>
<h4>作り方１：Yahoo!Pipesでフィードを作る</h4>
<p>まず、Yahoo!Pipesを使ってフィードを生成します。</p>
<p><a href="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/autotweet_pipes1.png" rel="lightbox"><img class="alignnone size-medium wp-image-570" title="つぶやき収集のPipes" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/autotweet_pipes1-300x237.png" alt="つぶやき収集のPipes" width="300" height="237" /></a></p>
<p>Yahoo!Pipesをイチから組むのはなかなか大変なんですが、<br />
私が作ったサンプルのPipesがあるのでそれを<br />
使えばとても簡単に作成することができます。</p>
<p>※Pipesを「Clone」すればカスタマイズできますので<br />
自分仕様にしたい場合はどうぞ！</p>
<p><a href="http://pipes.yahoo.com/msk_masaki/twitsearch" target="_blank">http://pipes.yahoo.com/msk_masaki/twitsearch</a></p>
<p>上記のYahoo!Pipesのページを開いて、<br />
興味のあるキーワードを入力して「Run Pipe」を押して、<br />
結果を確認してみてください。</p>
<p><img class="alignnone size-full wp-image-550" title="Yahoo!Pipesで検索" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/twitter_pipes_search1.png" alt="Yahoo!Pipesで検索" width="417" height="369" /></p>
<p>キーワードは、OR つながりで<br />
複数入力することもできます。<br />
"" でくくると、スペースがあっても単語が分割されません。</p>
<p>例：<br />
"EC studio" OR ecstudio OR ECスタジオ OR イーシースタジオ</p>
<p>結果が良さそうであれば、<br />
「Get as RSS」でフィードを取得します。</p>
<p><img class="alignnone size-full wp-image-577" title="Get RSS" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/twittersearch_getrss1.png" alt="Get RSS" width="496" height="146" /></p>
<p>※このフィードをRSSリーダーに登録しておいても便利です！</p>
<p>ここで表示されるフィードのURLをコピーしておいてください。</p>
<p>例：<br />
<a href="http://pipes.yahoo.com/pipes/pipe.run?_id=61e9c4c72fec86b4e16d0da3e0b3de8d&amp;_render=rss&amp;searchword=%22EC+studio%22+OR+ecstudio+OR+EC%E3%82%B9%E3%82%BF%E3%82%B8%E3%82%AA+OR+%E3%82%A4%E3%83%BC%E3%82%B7%E3%83%BC%E3%82%B9%E3%82%BF%E3%82%B8%E3%82%AA" target="_blank">"EC studio" OR ecstudio OR ECスタジオ OR イーシースタジオ のフィード</a></p>
<h4>作り方２：twitterfeedでつぶやかせる</h4>
<p>次に、Yahoo!Pipesで生成したフィードを使って、<br />
twitterfeedで自動でつぶやく設定をします。<br />
(<a href="http://feedtweet.am6.jp/" target="_blank">FeedTweet</a>では<span style="text-decoration: line-through;">URLの文字数の関係で出来ませんでした</span>)<br />
<strong>※できるようになったそうです！ </strong>(12/9)</p>
<p>twitterfeedを設定する前に、自動でつぶやくためのtwitterアカウントを<br />
新規で取得してください。</p>
<p><a href="http://www.twitter.com" target="_blank">http://www.twitter.com</a></p>
<p><strong>作成するアカウントは、必ず非公開にするようにしてください。</strong></p>
<p><img title="非公開に設定" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/twitter_private.png" alt="非公開に設定" width="561" height="237" /></p>
<p>twitterの右上にある「設定」から設定できます。</p>
<p>専用アカウントを作成したら、twitterfeedでアカウントを作成してください。</p>
<p><a href="http://twitterfeed.com" target="_blank">http://twitterfeed.com</a></p>
<p>作成したら、つぶやかせたいアカウントと連携させます。</p>
<p>「Create New Feed」から</p>
<p>OAuthを使ってtwitterアカウントと連携させます。<br />
(※先程作ったtwitterアカウントでログインしておきましょう)</p>
<p><img class="alignnone size-medium wp-image-551" title="twitterfeed認証" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/twitterfeed_authenticate-300x168.png" alt="twitterfeed認証" width="300" height="168" /></p>
<p>連携ができたら、フィードの設定を行います。</p>
<p><img class="alignnone size-full wp-image-552" title="twitterfeed設定" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/twitterfeed_setting.png" alt="twitterfeed設定" width="551" height="741" /></p>
<p><strong>Post Contentは「title only」で、<br />
Post Linkのチェックを外しておいてください。</strong></p>
<p>あとは設定を保存しておけば、自動でつぶやいてくれます！</p>
<p>twitterfeedは一定時間ごとに更新されますので、<br />
つぶやきがあってからしばらくタイムラグがあります。</p>
<h4>作り方３：メインのアカウントでフォローする</h4>
<p>これで興味のあるキーワードに関係するつぶやきを<br />
自動でつぶやくアカウントができたので、あとは<br />
自分のメインのアカウントでフォローすればOKです。</p>
<p>自分のタイムラインに流れてくるのはとても便利です！</p>
<p>他の人と共有するのも、フォローしておいてもらうだけ<br />
なので簡単です。</p>
<p>ぜひ活用してみてください！</p>
<h4>番外編： ツイートメールを利用する</h4>
<p>気になるアカウントやキーワードをメールでまとめて送信してくれる、<br />
ツイートメールというサービスをEC studioで開発しました！</p>
<p><a href="http://www.tweet-mail.jp" target="_blank">http://www.tweet-mail.jp</a></p>
<p><a href="http://www.tweet-mail.jp" target="_blank"><img class="alignnone size-full wp-image-584" title="ツイートメール" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/12/tweetmail.png" alt="ツイートメール" width="359" height="88" /></a></p>
<p>twitterアカウントが不要で、1日分がまとまってメールで送られてくるので<br />
とても便利です。私も使っています！</p>
<p>※よろしければ私のtwitterアカウントも<br />
フォローしてください <img src='http://techblog.ecstudio.jp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<a href="http://www.twitter.com/msk_masaki" target="_blank">http://www.twitter.com/msk_masaki</a></p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/tech-tips/ultraquick-capture-for-twitter.html" title="超高速でtwitterにキャプチャ画像をつぶやく仕組みを作ってみました (2010/02/22 月曜日)">超高速でtwitterにキャプチャ画像をつぶやく仕組みを作ってみました</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/autotweetcollector.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQueryとPHPでLinuxのtailコマンドを実装する</title>
		<link>http://techblog.ecstudio.jp/tech-tips/phptail.html</link>
		<comments>http://techblog.ecstudio.jp/tech-tips/phptail.html#comments</comments>
		<pubDate>Tue, 06 Oct 2009 09:40:17 +0000</pubDate>
		<dc:creator>山本 正喜</dc:creator>
				<category><![CDATA[PHP Tips]]></category>
		<category><![CDATA[技術ネタ]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[tail]]></category>

		<guid isPermaLink="false">http://techblog.ecstudio.jp/?p=522</guid>
		<description><![CDATA[Linuxにはtailというファイルの末尾10行程度を表示する、 ログファイルのチェックなどに便利なコマンドがあります。 オプションで一定間隔で常に末尾10行の表示を 更新し続けることもでき、ログの監視などに使えます。  [...]]]></description>
			<content:encoded><![CDATA[<p>Linuxにはtailというファイルの末尾10行程度を表示する、<br />
ログファイルのチェックなどに便利なコマンドがあります。</p>
<p>オプションで一定間隔で常に末尾10行の表示を<br />
更新し続けることもでき、ログの監視などに使えます。</p>
<p><img class="alignnone size-full wp-image-525" title="Linux tailコマンド" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/10/tail_linux.jpg" alt="Linux tailコマンド" width="484" height="233" /></p>
<p>ただ、とっても便利でサーバー管理者御用達のこのコマンド、<br />
当然ながらシェルでログインできなくては使えません。。</p>
<p>レンタルサーバーなどでシェル権限がない場合も多いですし、<br />
何よりもっとカンタンにブラウザから見たい！と思いませんか？</p>
<p>業務上の都合もありとっても欲しくなってので、作っちゃいました。</p>
<p>jQueryとPHPを使えば、思った以上にサクッと作れてしまったので、<br />
技術ブログでソースコードや解説も含めて公開します。<br />
(jQuery、メチャクチャ便利！です)</p>
<p><span id="more-522"></span></p>
<h3>jQuery + PHPでtailを作る！</h3>
<p>まずは今回の完成品です。</p>
<p><img class="alignnone size-full wp-image-526" title="phptail" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/10/phptail.png" alt="phptail" width="434" height="308" /></p>
<p>シンプルですね。<br />
「TAIL」ボタンを押すとtail実行、<br />
「STOP」ボタンを押すと更新が止まります。</p>
<p>実際に実行するとこうなります。<br />
※サンプルとしてこのプログラム自体をtailしました</p>
<p><img class="alignnone size-full wp-image-527" title="phptail 実行" src="http://techblog.ecstudio.jp/wp-content/uploads/2009/10/phptail_start.png" alt="phptail 実行" width="439" height="293" /></p>
<p>Ajaxで実装されており、2秒ごとに表示が更新されます。</p>
<p>では、ソースコードです。</p>
<div class="igBar"><span id="lphp-80"><a href="#" onclick="javascript:showCodeTxt('php-80'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-80">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">&lt;?php</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/header"><span style="color:#ff0000;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'Content-type:text/html; charset=UTF-8'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//対象のファイルパス</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$logpath</span> = <span style="color:#ff00ff;">'logfile.log'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//表示する末尾の行数</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$lines</span> = <span style="color:#CC66CC;color:#800000;">30</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//更新インターバル (ミリ秒)</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$interval</span> = <span style="color:#CC66CC;color:#800000;">2000</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/isset"><span style="color:#ff0000;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'load'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#ff00ff;">'file:'</span>.<a target="_blank"&nbsp; href="http://www.php.net/basename"><span style="color:#ff0000;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">' reload:'</span>.<a target="_blank"&nbsp; href="http://www.php.net/date"><span style="color:#ff0000;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'H:i:s'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">'&lt;hr size=&quot;1&quot;/&gt;'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a target="_blank"&nbsp; href="http://www.php.net/file_exists"><span style="color:#ff0000;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/die"><span style="color:#ff0000;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span>.<span style="color:#ff00ff;">'は存在しません'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span>read_tail<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span>,<span style="color:#663333;">$lines</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000ff;">as</span> <span style="color:#663333;">$i</span> =&gt; <span style="color:#663333;">$line</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$line</span> = <a target="_blank"&nbsp; href="http://www.php.net/rtrim"><span style="color:#ff0000;">rtrim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$line</span>,<span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\r</span><span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <a target="_blank"&nbsp; href="http://www.php.net/strtr"><span style="color:#ff0000;">strtr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/htmlspecialchars"><span style="color:#ff0000;">htmlspecialchars</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$line</span>,ENT_QUOTES<span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000ff;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\t</span>"</span> =&gt; <span style="color:#ff00ff;">'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$i</span> &lt;<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$lines</span> - <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#ff00ff;">'&lt;br /&gt;'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/exit"><span style="color:#ff0000;">exit</span></a>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/**</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * ログをファイルから指定行数読み出す</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * </span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * tail function by flash tekkie</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * http://tekkie.flashbit.net/php/tail-functionality-in-php</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * </span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @param string $file ファイルパス</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @param int $lines 行数</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> * @return array 行ごとの配列</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;"> */</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">function</span> read_tail<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$file</span>, <span style="color:#663333;">$lines</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//global $fsize;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$handle</span> = <a target="_blank"&nbsp; href="http://www.php.net/fopen"><span style="color:#ff0000;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$file</span>, <span style="color:#ff00ff;">"r"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$linecounter</span> = <span style="color:#663333;">$lines</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$pos</span> = -<span style="color:#CC66CC;color:#800000;">2</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$beginning</span> = <span style="color:#800000;">false</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$text</span> = <span style="color:#0000ff;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$linecounter</span>&gt; <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$t</span> = <span style="color:#ff00ff;">" "</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$t</span> != <span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/fseek"><span style="color:#ff0000;">fseek</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span>, <span style="color:#663333;">$pos</span>, SEEK_END<span style="color:#006600; font-weight:bold;">&#41;</span> == -<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$beginning</span> = <span style="color:#800000;">true</span>; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">break</span>; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$t</span> = <a target="_blank"&nbsp; href="http://www.php.net/fgetc"><span style="color:#ff0000;">fgetc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$pos</span> --;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$linecounter</span> --;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$beginning</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/rewind"><span style="color:#ff0000;">rewind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$text</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#663333;">$lines</span>-<span style="color:#663333;">$linecounter</span>-<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <a target="_blank"&nbsp; href="http://www.php.net/fgets"><span style="color:#ff0000;">fgets</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$beginning</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000ff;">break</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/fclose"><span style="color:#ff0000;">fclose</span></a> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">return</span> <a target="_blank"&nbsp; href="http://www.php.net/array_reverse"><span style="color:#ff0000;">array_reverse</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$text</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">?&gt;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;html xmlns=<span style="color:#ff00ff;">"http://www.w3.org/1999/xhtml"</span> xml:lang=<span style="color:#ff00ff;">"ja"</span> lang=<span style="color:#ff00ff;">"ja"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;meta name=<span style="color:#ff00ff;">"robots"</span> content=<span style="color:#ff00ff;">"noindex,nofollow"</span> /&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#ff00ff;">"text/javascript"</span> src=<span style="color:#ff00ff;">"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#ff00ff;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">var</span> timer = <span style="color:#800000;">null</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">function</span> start_tail<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>timer<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; clearInterval<span style="color:#006600; font-weight:bold;">&#40;</span>timer<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'#status'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.html<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'running...'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; timer = setInterval<span style="color:#006600; font-weight:bold;">&#40;</span>run,<span style="color:#800000;">&lt;?php</span> <span style="color:#0000ff;">echo</span> <span style="color:#663333;">$interval</span> <span style="color:#800000;">?&gt;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">function</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'#console'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.load<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'?load=1&amp;m='</span> + <span style="color:#800000;">new</span> <a target="_blank"&nbsp; href="http://www.php.net/date"><span style="color:#ff0000;">Date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.getTime<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">function</span> stop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; clearInterval<span style="color:#006600; font-weight:bold;">&#40;</span>timer<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'#status'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<a target="_blank"&nbsp; href="http://www.php.net/empty"><span style="color:#ff0000;">empty</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;">&lt;/script&gt;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;style type=<span style="color:#ff00ff;">"text/css"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">#console {</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; width:800px;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; height:500px;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; overflow:scroll;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; border:1px solid <span style="color:#008000;">#999999;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; font-size:12px;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; font-family:consolas;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/style&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/head&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;input type=<span style="color:#ff00ff;">"button"</span> onclick=<span style="color:#ff00ff;">"start_tail()"</span> value=<span style="color:#ff00ff;">"TAIL"</span>&gt; &lt;input type=<span style="color:#ff00ff;">"button"</span> onclick=<span style="color:#ff00ff;">"stop()"</span> value=<span style="color:#ff00ff;">"STOP"</span>&gt; &lt;span id=<span style="color:#ff00ff;">"status"</span>&gt;&lt;/span&gt;&lt;br /&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;pre id=<span style="color:#ff00ff;">"console"</span>&gt;&lt;/pre&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/body&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/html&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>これだけ！です。<br />
外部ファイルも不要なので、このコードを<br />
phptail.phpなどで保存して、実行すればokです。(文字コードはUTF-8で)</p>
<p>たった100行ぐらいでAjaxを使ったtailを実装できちゃうところが、<br />
jQueryとPHPの素晴らしいところですね。</p>
<p>では、ソースコードの解説をしてみます。</p>
<h3>PHPの処理を作る</h3>
<p>今回のキモはなんといってもtail部分。<br />
ファイルを末尾から読み出すのは結構難しいです。</p>
<p>こういった処理を自分で作ってもいいですが、<br />
海外サイトに良さそうなコードがあったのでお借りしましょう。</p>
<p>「php tail」でGoogle検索すればすぐ見つかりました。<br />
<a href="http://tekkie.flashbit.net/php/tail-functionality-in-php" target="_blank">flash tekkie » Blog Archive » tail functionality in PHP</a></p>
<p>今回はここのread_file関数を使うことにします。<br />
read_fileだと少々わかりにくいのでread_tailに変更しました。</p>
<div class="igBar"><span id="lphp-81"><a href="#" onclick="javascript:showCodeTxt('php-81'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-81">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">function</span> read_tail<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$file</span>, <span style="color:#663333;">$lines</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000;">//global $fsize;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$handle</span> = <a target="_blank"&nbsp; href="http://www.php.net/fopen"><span style="color:#ff0000;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$file</span>, <span style="color:#ff00ff;">"r"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$linecounter</span> = <span style="color:#663333;">$lines</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$pos</span> = -<span style="color:#CC66CC;color:#800000;">2</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$beginning</span> = <span style="color:#800000;">false</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#663333;">$text</span> = <span style="color:#0000ff;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$linecounter</span>&gt; <span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$t</span> = <span style="color:#ff00ff;">" "</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$t</span> != <span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/fseek"><span style="color:#ff0000;">fseek</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span>, <span style="color:#663333;">$pos</span>, SEEK_END<span style="color:#006600; font-weight:bold;">&#41;</span> == -<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$beginning</span> = <span style="color:#800000;">true</span>; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">break</span>; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$t</span> = <a target="_blank"&nbsp; href="http://www.php.net/fgetc"><span style="color:#ff0000;">fgetc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$pos</span> --;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$linecounter</span> --;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$beginning</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/rewind"><span style="color:#ff0000;">rewind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$text</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#663333;">$lines</span>-<span style="color:#663333;">$linecounter</span>-<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <a target="_blank"&nbsp; href="http://www.php.net/fgets"><span style="color:#ff0000;">fgets</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$beginning</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000ff;">break</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/fclose"><span style="color:#ff0000;">fclose</span></a> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000ff;">return</span> <a target="_blank"&nbsp; href="http://www.php.net/array_reverse"><span style="color:#ff0000;">array_reverse</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$text</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>この関数に読み出したいファイルパスを渡して、<br />
それを一定秒数ごとに表示してやればokですね。</p>
<h3>Ajaxで定期的に読み込む</h3>
<p>Ajaxというと難しいイメージがありますが、<br />
jQueryを使えばほんとにカンタンに実装できてしまいます。</p>
<p>今回は<br />
「TAIL」ボタンにstart_tail()を、<br />
「STOP」ボタンにstop()を、<br />
onclick属性でそれぞれ割り当てました。</p>
<p>JavaScriptの関数コードは</p>
<div class="igBar"><span id="ljavascript-82"><a href="#" onclick="javascript:showCodeTxt('javascript-82'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-82">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color: #3366CC;">"text/javascript"</span> src=<span style="color: #3366CC;">"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color: #3366CC;">"text/javascript"</span>&gt;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> timer = <span style="color: #003366; font-weight: bold;">null</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> start_tail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>timer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; clearInterval<span style="color: #66cc66;">&#40;</span>timer<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#status'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'running...'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; timer = setInterval<span style="color: #66cc66;">&#40;</span>run,&lt;?php echo $interval ?&gt;<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#console'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'?load=1&amp;m='</span> + <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; clearInterval<span style="color: #66cc66;">&#40;</span>timer<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#status'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>これだけです。<br />
はじめにajax.googleapis.comからjQueryのライブラリを読み込みます。</p>
<p>これはGoogleが無償で提供しているGoogle AJAX Libraries APIというもので、<br />
Googleがホストしてくれている様々なJavaScriptライブラリを<br />
自由に使ってかまわないというものです。</p>
<p><a href="http://code.google.com/intl/ja/apis/ajaxlibs/" target="_blank">http://code.google.com/intl/ja/apis/ajaxlibs/</a></p>
<p>これのおかげで、jquery.jsなどを別途用意する必要がありません。<br />
(Googleに感謝！)</p>
<p>次に、start_tailを見てみると、<br />
setIntervalでrunという関数を定期的に実行しています。</p>
<p>このrunという関数が実行される度に、Ajaxで先ほどの<br />
PHPで作ったtailの処理が実行されます。</p>
<div class="igBar"><span id="ljavascript-83"><a href="#" onclick="javascript:showCodeTxt('javascript-83'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="javascript-83">
<div class="javascript">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#console'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'?load=1&amp;amp;m='</span> + <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>ここが、Ajaxです。なんと1行！</p>
<p>これは、id="console"のタグの中身に対して、<br />
'?load=1&amp;m=タイムスタンプ' のURLの実行結果を読み込みます。</p>
<p>?より前が省略されているのは、自分自身(php)を実行するためです。<br />
?でload=1というパラメータをつけることにより、<br />
PHP側で、</p>
<div class="igBar"><span id="lphp-84"><a href="#" onclick="javascript:showCodeTxt('php-84'); return false;">テキスト表示</a></span></div>
<div class="syntax_hilite">
<div id="php-84">
<div class="php">
<ol>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/isset"><span style="color:#ff0000;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff00ff;">'load'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#ff00ff;">'file:'</span>.<a target="_blank"&nbsp; href="http://www.php.net/basename"><span style="color:#ff0000;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">' reload:'</span>.<a target="_blank"&nbsp; href="http://www.php.net/date"><span style="color:#ff0000;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">'H:i:s'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#ff00ff;">'&lt;hr size=&quot;1&quot;/&gt;'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a target="_blank"&nbsp; href="http://www.php.net/file_exists"><span style="color:#ff0000;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/die"><span style="color:#ff0000;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span>.<span style="color:#ff00ff;">'は存在しません'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span>read_tail<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$logpath</span>,<span style="color:#663333;">$lines</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#0000ff;">as</span> <span style="color:#663333;">$i</span> =&gt; <span style="color:#663333;">$line</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#663333;">$line</span> = <a target="_blank"&nbsp; href="http://www.php.net/rtrim"><span style="color:#ff0000;">rtrim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$line</span>,<span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\r</span><span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <a target="_blank"&nbsp; href="http://www.php.net/strtr"><span style="color:#ff0000;">strtr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a target="_blank"&nbsp; href="http://www.php.net/htmlspecialchars"><span style="color:#ff0000;">htmlspecialchars</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$line</span>,ENT_QUOTES<span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000ff;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff00ff;">"<span style="color:#000099; font-weight:bold;">\t</span>"</span> =&gt; <span style="color:#ff00ff;">'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$i</span> &lt;<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#663333;">$lines</span> - <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000ff;">echo</span> <span style="color:#ff00ff;">'&lt;br /&gt;'</span>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#FFFFFF;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a target="_blank"&nbsp; href="http://www.php.net/exit"><span style="color:#ff0000;">exit</span></a>;</div>
</li>
<li style="font-family: consolas,'Courier New', Courier, monospace;margin-left:25px;padding:1px 1px 1px 3px;background-color:#F8F8F8;">
<div style="font-family: consolas,'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>このように、Ajaxから呼ばれた場合のみに<br />
tail処理を行わせてexitさせる　ということをしています。</p>
<p>Ajax用に別のphpを用意してもいいのですが、<br />
簡単な処理であれば自分自身を呼び出す方が<br />
ソースコードもまとまり便利です。</p>
<p>"m=タイムスタンプ"の部分は、IEの場合<br />
Ajaxで同じURLを連続して読み出した場合に<br />
キャッシュされてしまうため、URLを毎回変化させています。<br />
PHP側ではこのmの値は全く使用しません。</p>
<p>今回のtailではブラウザ互換の都合上、<br />
タブはスペース4つに置換されるという仕様にしています。<br />
(まあ、問題児はもちろんIEなんですが、、)</p>
<p>IE8,FireFox3.5,Chrome3 で動作確認しています。</p>
<p>問題がある場合はソースを修正してください。</p>
<h3>PHPで作ることの意味、応用アイデア</h3>
<p>以上でソースコードの説明は終わりです。</p>
<p>とても短いソースコードなので、業務の用途にあわせて<br />
改変していただければなと思います。</p>
<p>今回はとてもシンプルにただ1つのファイルを監視するだけですが、<br />
PHPを改造すればローテートされる複数のログファイルを横断したり、<br />
正規表現で抜き出したり、自由自在にファイルを<br />
監視するスクリプトを作れるのではないかと思います。</p>
<p>ただ注意しなければいけないのが、監視対象のファイルを<br />
GET値などで指定できるようにすると、セキュリティ上<br />
非常に危ないのでそういった改造をする場合は<br />
注意するようにしてください。</p>
<p>このソースコードは自由に使用していただいて構いませんが、<br />
各自の自己責任での使用をお願いします！ <img src='http://techblog.ecstudio.jp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

	<br />関連した記事：
	<ul class="st-related-posts">
	<li><a href="http://techblog.ecstudio.jp/tech/attractive-jquery.html" title="プログラマ視点でみたjQueryの魅力 (2010/03/09 火曜日)">プログラマ視点でみたjQueryの魅力</a></li>
	<li><a href="http://techblog.ecstudio.jp/news/vol5.html" title="週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ (2008/02/22 金曜日)">週間TechNews vol.5 &#8211; Officeバイナリ仕様オープン化,Skype TODO管理,YUIバージョンアップ</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/oo-javascript-for-phper.html" title="PHPエンジニアのためのオブジェクト指向JavaScriptの書き方 (2010/06/22 火曜日)">PHPエンジニアのためのオブジェクト指向JavaScriptの書き方</a></li>
	<li><a href="http://techblog.ecstudio.jp/tech-tips/google-ajax-libraries-api-updated.html" title="Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に (2009/01/19 月曜日)">Google Ajax Libraries APIにSWFObject、YUIが追加！jQueryも1.3に</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://techblog.ecstudio.jp/tech-tips/phptail.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

