<?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>We were born in 1967. &#187; Google Custom Search</title>
	<atom:link href="http://www.haru3-67.com/tag/google-custom-search/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haru3-67.com</link>
	<description>1967年式 Volkswagen Type-1 に乗る、1967年生まれのおやじの blog です。</description>
	<lastBuildDate>Sun, 09 Oct 2011 01:46:57 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Google Custom Search の JavaScript OFF対応を jQuery で</title>
		<link>http://www.haru3-67.com/2010/06/290.html</link>
		<comments>http://www.haru3-67.com/2010/06/290.html#comments</comments>
		<pubDate>Sun, 20 Jun 2010 15:26:13 +0000</pubDate>
		<dc:creator>haru3</dc:creator>
				<category><![CDATA[Web制作]]></category>
		<category><![CDATA[Google Custom Search]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.haru3-67.com/?p=290</guid>
		<description><![CDATA[Google Custom Search の iframe で表示させる方法は、JavaScript を無効にされている環境では検索結果が表示されません。そこで JavaScript が有効な環境では iframe 方式 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.google.com/cse/?hl=ja">Google Custom Search</a> の iframe で表示させる方法は、JavaScript を無効にされている環境では検索結果が表示されません。そこで JavaScript が有効な環境では iframe 方式で、JavaScript が無効な環境では Google でホストされるページで検索結果を表示させる手段として、これまで <a href="http://www.haru3-67.com/2008/09/145.html">prototype.js を使った方法</a>を、サイト制作の中で使ってきました。</p>
<p>でも最近 <a href="http://jquery.com/">jQuery</a> を利用したライブラリが増えてきたことと、自分でも jQuery を勉強したいということで、JavaScript ON/OFF による Google Custom Search のコードの書き替えを jQuery 化してみました。</p>
<p><span id="more-290"></span>まず、HTML 側の検索フォーム部分は以下のとおりで、Google でホストされるページで表示するオリジナルのソースコードと同じです。</p>
<pre><code>&lt;form action=&quot;http://www.google.com/cse&quot; id=&quot;cse-search-box&quot;&gt;
&lt;div&gt;
&lt;input type=&quot;hidden&quot; name=&quot;cx&quot; value=&quot;**********&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;ie&quot; value=&quot;UTF-8&quot; /&gt;
&lt;input type=&quot;text&quot; name=&quot;q&quot; size=&quot;31&quot; /&gt;
&lt;input type=&quot;submit&quot; name=&quot;sa&quot; value=&quot;検索&quot; /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en&quot;&gt;&lt;/script&gt;</code></pre>
<p>そして、JavaScript が有効な場合にこれを書き替えるための jQuery を使った記述がこれ。</p>
<pre><code>$(function(){
  $(&quot;#cse-search-box&quot;).attr(&quot;action&quot;,&quot;（検索結果を表示する URL）&quot;);
  $(&quot;#cse-search-box div&quot;).prepend(&quot;&lt;input type='hidden' name='cof' value='FORID:11' /&gt;&quot;);
});</code></pre>
<p>2行目で <code>id=&quot;cse-search-box&quot;</code> が付けられた要素の <code>action</code> 属性を検索結果を表示するURL に書き替え、3行目で <code>id=&quot;cse-search-box&quot;</code> が付けられた要素の中にある <code>div</code> 要素の先頭に <code>&lt;input type='hidden' name='cof' value='FORID:11' /&gt;</code> を追加しています。</p>
<p>これを <code>head</code> 要素の中で <a href="http://code.jquery.com/jquery-1.4.2.min.js">jQuery</a> を読み込んだ後に読み込めば OK。prototype.js を使うよりさらにシンプルになりました。</p>
<p>なお、このブログは lightbox 等に prototype.js を使用しているため、まだ jQuery 化していません。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haru3-67.com/2010/06/290.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

