<?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>Fireproof Socks &#187; Ajax</title>
	<atom:link href="http://www.fireproofsocks.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fireproofsocks.com</link>
	<description>Audio, Computers, and Stuff...</description>
	<lastBuildDate>Sun, 10 Aug 2008 07:47:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Javascript document.write for SEO</title>
		<link>http://www.fireproofsocks.com/php/using-javascript-documentwrite-for-seo/</link>
		<comments>http://www.fireproofsocks.com/php/using-javascript-documentwrite-for-seo/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 08:30:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.fireproofsocks.com/php/using-javascript-documentwrite-for-seo/</guid>
		<description><![CDATA[The idea here is that search-engines read a bunch of text on your page that may not be relevant.  You can use javascript to write text to a page on the client-side, and that text won&#8217;t be processed by the search-engines, thus achieving Search Engine Optimization (SEO).
There are a couple ways to do this&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>The idea here is that search-engines read a bunch of text on your page that may not be relevant.  You can use javascript to write text to a page on the client-side, and that text won&#8217;t be processed by the search-engines, thus achieving Search Engine Optimization (SEO).</p>
<p>There are a couple ways to do this&#8230; the easiest is using javascript&#8217;s document.write:</p>
<p><code><br />
&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;document.write('Hello World');<br />
&lt;/script&gt;<br />
</code></p>
<p>This works great for simple little messages, but what if you have a large multi-line text string?  What if your text has HTML tags and double quotes in it?  Then you&#8217;re in for some trouble, because the nice little javascript examples will die on you most ingloriously.  PHP to the rescue.  Use PHP to replace the spaces (and double-quotes)&#8230; you may have to escape your double-quotes in the source text.  </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-5121424398135626";
/* 468x60, created 3/15/08 */
google_ad_slot = "6666906371";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Extending that simple little example with some meaty text and some PHP, you end up with something like this:</p>
<p><code><br />
&lt;?php</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;$text = "<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;big, multi-line<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"stuff\", watch out!<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/p&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;";</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;$text = preg_replace("/\s+/"," ",$text);<br />
&nbsp;&nbsp;&nbsp;&nbsp;$text = preg_replace('/\\"/',"''",$text);     </p>
<p>?&gt;</p>
<p>                &lt;script type="text/javascript"&gt;<br />
                        &nbsp;&nbsp;&nbsp;&nbsp;document.write('&lt;?php echo $text; ?&gt;');<br />
                &lt;/script&gt;</p>
<p></code></p>
<p>That solution works (at least, it did before I had to replace all the tags with html entities to make this post).  The text gets written at that point in the document once it loads on the client-side.  Notice that the regular expressions replace double-quotes with 2 single quotes (tricky, eh?), and any excessive space is replaced.  But what if you want something more complicated&#8230; what if you don&#8217;t like the regular expressions and having to escape your double-quotes?  The above method is sorta techie for some, and prone to error.  There is another solution: use an Ajax library to pipe HTML directly to a div tag.  This solution is more scalable because you can put all the HTML in a separate file, and you don&#8217;t have to escape characters.</p>
<p>Download this Javascript library from <a href="http://www.prototypejs.org/">http://www.prototypejs.org/</a> and copy the file somewhere in your site&#8217;s html directory (here I called it div_updater.js and put it in my js directory).  Reference the file in a script tag using the &#8217;src&#8217; element, then use the Ajax.Updater function to specify 1) the div id where the text should be written and 2) the file to be included.</p>
<p><code><br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id="target_div"&gt;&lt;/div&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript" src="js/div_updater.js"&gt;&lt;/script&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Ajax.Updater('target_div', 'include_file.php');<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/script&gt;<br />
</code></p>
<p>This solution will write the text to where-ever you have the target_div.  Just make sure you use give the div an &#8216;id&#8217; and that its name is referenced correctly by the Updater function.  Using one of these solutions, you can utilize javascript to hide text from the search engines.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-5121424398135626";
/* 468x60, created 3/15/08 */
google_ad_slot = "6666906371";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.fireproofsocks.com%2Fphp%2Fusing-javascript-documentwrite-for-seo%2F';
  addthis_title  = 'Using+Javascript+document.write+for+SEO';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.fireproofsocks.com/php/using-javascript-documentwrite-for-seo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
