<?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>Web Cash &#187; javascript</title>
	<atom:link href="http://www.earn-web-cash.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Use Javascript to Parse a Query String</title>
		<link>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/</link>
		<comments>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 04:19:33 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/</guid>
		<description><![CDATA[Most server-side scripting languages, like PHP, come with built-in functionality for reading query strings. Javascript doesn&#8217;t have any kind of standard counterpart, but that doesn&#8217;t mean you can&#8217;t use query strings in Javascript. With some String functions, you can create your own function to parse a query string attached to the request url and store [...]]]></description>
			<content:encoded><![CDATA[<p>Most server-side scripting languages, like PHP, come with built-in functionality for reading query strings.  Javascript doesn&#8217;t have any kind of standard counterpart, but that doesn&#8217;t mean you can&#8217;t use query strings in Javascript.</p>
<p>With some String functions, you can create your own function to parse a query string attached to the request url and store it in an associative array &#8211; just like PHP would for you.<br />
<span id="more-171"></span></p>
<h4>What Are Query Strings For</h4>
<p>If you&#8217;re developing anything more than a basic informative page, you&#8217;ll need to pass information from one page to the next.  You might want to pass a search query, some form values, the answer to a quiz question, or any number of other things.  Passing this information is the first step in converting your page from a static bit of information into a dynamic application.</p>
<p>One way to pass information between pages is to use a query string.  That&#8217;s the long line of text after the &#8216;?&#8217; in a URL.  Kind of like this.</p>
<pre>http://www.google.com/search?hl=en&#038;q=widgets&#038;btnG=Google+Search</pre>
<p>That&#8217;s a standard query string attached to a Google search.  It sends a few values to the new page &#8211; hl (en), q (widgets), and btn (Google Search).  These values are then used to determine what is displayed on the new page.</p>
<p>In PHP, these values are automatically stored in an associative array &#8211; the $_GET array.  To make these values easily available to our Javascript code, we can write a function to do the same thing.</p>
<h4>The Structure of a Query String</h4>
<p>In order to break a query string down, we need to understand the rules for building one.</p>
<p>A query string always starts with &#8216;?&#8217;.  This separates the query string from the actual URL.</p>
<p>This is followed by key-value pairs, like &#8220;key=value&#8221;.  If there is more than one key-value pair, they are separated by an ampersand &#8211; &#8220;key1=value1&amp;key2=value2&#8243;.</p>
<p>Finally, these values are url_encoded.  Special characters &#8211; like spaces, ampersands, and equal signs &#8211; are converted to code so that they don&#8217;t interfere with the URL or the query string.</p>
<h4>Step One: Get the Request String and Separate the Query</h4>
<p>The first thing we need to do is get the entire request string and separate out just the query string.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//  Fetch the entire request string</span>
<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> hrefParts <span style="color: #339933;">=</span> href.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  [0] is the URL, [1] is the query string</span>
<span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>hrefParts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The request string is accessible through the window.location object.  We use the split method of the string class to explode it at the &#8216;?&#8217;.  This leaves us with two parts, and we save the entire query string in the variable <code>query</code>.</p>
<h4>Step Two: Get the Key Value Pairs</h4>
<p>At this point, our query string in the variable <code>query</code> might look something like this:</p>
<pre>hl=en&#038;q=widgets&#038;btnG=Google+Search</pre>
<p>To get the key-value pairs, we can use another split() call.  This time, we split the string at each ampersand (&amp;).  The resulting array will contain the strings &#8220;hl=en&#8221;, &#8220;q=widgets&#8221;, and &#8220;btnG=Google+Search&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Step Three: Loop Through and Create an Array</h4>
<p>Now we can loop through the array valuePairs, split each pair, and create a new array where each value is indexed by its key.  This gives us a nicely indexed array just like PHP would.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> each <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #000066; font-weight: bold;">in</span> valuePairs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// [0] = key, [1] = value</span>
  <span style="color: #003366; font-weight: bold;">var</span> tempPair <span style="color: #339933;">=</span> pair.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Unescape converts the text back from URL encoding</span>
  getArray<span style="color: #009900;">&#91;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>At this point, getArray[] should hold all of the values in our query string.</p>
<h4>Why Bother?</h4>
<p>Good question.  If PHP (and other server side scripts) automatically parse query strings, why should we bother using Javascript for it?</p>
<p>One reason may be to use query strings to pass information on pages that don&#8217;t have any server side scripting enabled.  I was looking into a web design competition for high school students, and it doesn&#8217;t allow any server-side scripts &#8211; only HTML, CSS, and Javascript.</p>
<p>With this method, we could use query strings to bring a little dynamism to the party.</p>
<p>This is also helpful for reading query strings in general &#8211; not just the one in the request string.  For example, if you use Ajax to get a value from a php script, how is the output formatted?</p>
<p>It may be plain text if it&#8217;s just one variable, or it may be in an XML format.  If the output is just a handful of key-value pairs, it might be easier and more efficient to create a query string and parse it with this function.</p>
<p>Or&#8230; maybe it&#8217;s just fun to say &#8220;I can do this.&#8221;  That&#8217;s a good enough reason for me.</p>
<p>And as usual, here&#8217;s the full function, in case you had some trouble.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> parseQueryString<span style="color: #009900;">&#40;</span>fullHref<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> hrefParts <span style="color: #339933;">=</span> fullHref.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  [0] is the URL, [1] is the query string</span>
  <span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>hrefParts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span> each <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #000066; font-weight: bold;">in</span> valuePairs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// [0] = key, [1] = value</span>
    <span style="color: #003366; font-weight: bold;">var</span> tempPair <span style="color: #339933;">=</span> pair.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Unescape converts the text back from URL encoding</span>
    getArray<span style="color: #009900;">&#91;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> getArray<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  To use it, pass the request string or another href</span>
<span style="color: #006600; font-style: italic;">//  to the function.  It returns the array.</span>
<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> parseQueryString<span style="color: #009900;">&#40;</span>href<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Randomize AdSense Ads with Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 23:45:30 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Advertising]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ad]]></category>
		<category><![CDATA[AdSense]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</guid>
		<description><![CDATA[I came across this problem on the forums today. How can you randomize which Ad Sense ad to show using Javascript? The solution, it turns out, is very simple. Why Randomize Ads? There are a few reasons you might want to do this. If you&#8217;re looking into changing the theme of your AdSense ad, you [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the forums today.  How can you randomize which Ad Sense ad to show using Javascript?</p>
<p>The solution, it turns out, is very simple.<br />
<span id="more-164"></span></p>
<h4>Why Randomize Ads?</h4>
<p>There are a few reasons you might want to do this.</p>
<p>If you&#8217;re looking into changing the theme of your AdSense ad, you may want to test the two themes against each other.  By giving each theme approximately 50% of the impressions for a given time period, you can make some direct comparisons between their performance.</p>
<p>Or maybe you and a friend both run a website, so you want to split the advertising time between two AdSense publisher ids.  That can be done too.</p>
<h4>How the AdSense Code Works</h4>
<p>Before we randomize it, let&#8217;s take a look at how the AdSense code works.</p>
<p>Here&#8217;s the snippet of code that creates the skyscraper on the left side of this page.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">google_ad_client = &quot;pub-2399151883698113&quot;;</span>
<span style="color: #808080; font-style: italic;">/* Web Cash Tall Skyscraper */</span>
<span style="color: #808080; font-style: italic;">google_ad_slot = &quot;8702750734&quot;;</span>
<span style="color: #808080; font-style: italic;">google_ad_width = 160;</span>
<span style="color: #808080; font-style: italic;">google_ad_height = 600;</span>
<span style="color: #808080; font-style: italic;">//--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>You&#8217;ll notice two major pieces to this code.  The first piece &#8211; in the first <code>&lt;script&gt;</code> element &#8211; defines the ad unit&#8217;s properties.  It sets google_ad_client (your publisher id), google_ad_slot (the saved ad unit&#8217;s id with color info), google_ad_width, and google_ad_height.</p>
<p>The second section &#8211; the script element with src &#8220;show_ads.js&#8221; &#8211; actually displays the ad based on the variables that were set previously.</p>
<p>In order to vary the type of ad shown, we need to come up with a conditional statement to vary the google_ad_client/slot/width/height settings.</p>
<h4>The Javascript Code&#8230;</h4>
<p>And here&#8217;s the moment you&#8217;ve all been waiting for &#8211; the Javascript code to randomize which ad is shown.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5396010225&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3900536874&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//--&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
&lt;/script&gt;</pre></div></div>

<p>You&#8217;ll notice that there&#8217;s an if/else statement, and each branch of the conditional statement defines a different ad to display.  The results of that conditional statement rely on the outcome of Math.random().</p>
<p>Previously, we built a <a href="http://www.earn-web-cash.com/2008/02/24/random-number-function">function to get a useful random number out of Math.random()</a>.  This is actually a situation where we can use the value from Math.random() just the way it is.</p>
<p>Math.random() generates a random value between 0 and 1.  In other words, it gives you a probability.  By checking that Math.random() is greater than 0.5, the statement should evaluate to true approximately 50% of the time.</p>
<p>We could also make the rotation more lop-sided.  For example (Math.random() > 0.70) would be true 30% of the time.  Therefore the first ad would be displayed 30% of the time, while the second ad would be displayed 70% of the time.</p>
<p>I hope you weren&#8217;t looking for something more complicated because, well, it isn&#8217;t.  It&#8217;s just that simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Javascript Function: Random Number Generator</title>
		<link>http://www.earn-web-cash.com/2008/02/24/random-number-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/random-number-function/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:07:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/random-number-function/</guid>
		<description><![CDATA[Yesterday, I wrote a short article on generating random numbers in Javascript. The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful. At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a short <a href="http://www.earn-web-cash.com/2008/02/23/random-number-javascript/">article on generating random numbers in Javascript</a>.  The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful.</p>
<p>At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the general formula for this into a short helper function to generate a random number.<br />
<span id="more-158"></span></p>
<h4>Random Numbers: General Formula</h4>
<p>First, let&#8217;s take a minute and recap the general formula for finding a random number.</p>
<p>Javascript&#8217;s Math.random() method returns a random number between 0 and 1.  By multiplying this decimal value by a whole number, we can generate a desired range of values.  For example, by multiplying Math.random()&#8217;s result by 10, we get a range of 0 to 10.</p>
<p>Next, we take the Math.floor value of that random number.  This gives us just the integer portion &#8211; and it effectively cuts our range to 0 through 9.  Finally, we can modify this by adding an offset number.  So if we wanted a random number between 51 and 60, we&#8217;d add 51 to that result.</p>
<h4>Creating a Random Number Function</h4>
<p>The basic formula we end up with is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Our code would be much more readable if we had a helper function that could be called like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">randomNumber <span style="color: #339933;">=</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function is going to take two parameters &#8211; min and max.  From these two parameters, we can calculate the offset and the set size and generate the proper random number.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> min<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> min<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> randomNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> range<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> offset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> randomNumber<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All done.</p>
<p>Add that function to a utility file, and you&#8217;ve got a handy random number generator at your disposal.  It even uses the same format as PHP&#8217;s rand() function, to help increase consistency and readability in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/random-number-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Number in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 04:34:19 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</guid>
		<description><![CDATA[I've been working with PHP for some time now, and I've become spoiled with the rand() function for generating random numbers.  It's so simple - pass a min, pass a max, and get the number you want.

While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It's not quite as simple as the rand() function - we'll have to go back to the old C-style random number generation.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with PHP for some time now, and I&#8217;ve become spoiled with the rand() function for generating random numbers.  It&#8217;s so simple &#8211; pass a min, pass a max, and get the number you want.</p>
<p>While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It&#8217;s not quite as simple as the rand() function &#8211; we&#8217;ll have to go back to the old C-style random number generation.<br />
<span id="more-156"></span></p>
<h4>The Math Object and Random Numbers</h4>
<p>Javascript comes with a built-in random number generator as part of the Math object.  This Math object does a number of calculations &#8211; trig functions (sine, cosine, and tangent), rounding, logarithms, exponents, etc.</p>
<p>To use the Math class to generate a random number, you begin with the random method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This method returns a random value &#8211; between 0 and 1.</p>
<h4>How Can I Make a Useful Random Number?</h4>
<p>If you&#8217;ve ever programmed in C/C++, you&#8217;ll know what to do.  If you&#8217;re used to working with something like PHP &#8211; which comes with an intelligent built-in random number generator &#8211; you may be left in the dark.</p>
<p>How can you take a random decimal &#8211; between 0 and 1 &#8211; and turn it into a random number between, say, 1 and 100?</p>
<p>The answer is simple &#8211; math.  You need to multiply the random() result to get a certain sized range and then use addition to start the range at the right number.</p>
<p>Let&#8217;s say you multiply the result of Math.random() by 10.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span></pre></div></div>

<p>Since the results of Math.random() are between 0 and 1, the results of this statement will be 0 to 10.  The minimum result is (0 * 10 = 0) and the highest result is (1 * 10 = 10).</p>
<p>If you were to only look at the whole number part of the result, you would get a range of 0 to 9.  In actuality, the result of Math.random() is never 0 or 1, so the above statement won&#8217;t give you a result of 10.</p>
<p>Therefore we can use the whole number portion of the result to get a random integer &#8211; 0 to 9.</p>
<h4>How Do We Get a Whole Number?</h4>
<p>Simple.  Use the floor method of the Math class.  Math.floor returns the whole number portion of a float/decimal.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//  Result is 1</span></pre></div></div>

<p>Simply pass a float/decimal to the Math.floor method, and it will return the integer portion of that number.  If you&#8217;re working with negative numbers, it will always round down.  Keep that in mind.  For positive numbers, it&#8217;s simply a truncation function.</p>
<p>To get a random integer, we simply pass the result of Math.rand() to Math.floor().</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>What If My Random Set Doesn&#8217;t Start at 0?</h4>
<p>Here&#8217;s the last piece of the random puzzle.</p>
<p>You can determine the size of your random number set with multiplication.  The coefficient you use (like we used 10 before) determines the range your random number can be in.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 Numbers, 0-9</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 5 Numbers, 0-4</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 100 Numbers, 0-99</span></pre></div></div>

<p>If the set you want to create a random number from doesn&#8217;t start with 0, you simply use addition to get what you want.</p>
<p>Let&#8217;s say you want a random number from 101-150.  Your set contains 50 numbers, and it starts with 101.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">101</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 101 - 150</span></pre></div></div>

<p>The basic statement &#8211; Math.floor( Math.rand() * 50) &#8211; returns a value between 0 and 49.  We  want to offset that by 101 places and we&#8217;ll have our desired random set &#8211; 101 to 150.</p>
<p>At this point, you should have a general equation you can use for generating random numbers from any given set.  Take Math.rand(), apply a coefficient to determine the size of the random number set, and then use addition to offset that number to your desired starting point.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Now go find something useful to use these random numbers for.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Javascript Game Spotlight: 3d Worm</title>
		<link>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 15:09:07 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/</guid>
		<description><![CDATA[I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.

One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It's a good example of what you can accomplish with some Javascript/DOM trickery.]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.png' title='3d Worm Screenshot'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/3dworm.thumbnail.png' alt='Screenshot of a 3d Worm game written in Javascript.' /></a>I was browsing the <a href="http://www.frihost.com" rel="nofollow">Frihost</a> forums this morning, and I stumbled on a cool Javascript game.</p>
<p>One of the Frihost users and his friend made a <a href="http://tekage.com.ar/uploaded/3Dworm/">3d Worm game</a>.  Although the directions are in Spanish (as is the source code), the game is pretty cool.  It&#8217;s a good example of what you can accomplish with some Javascript/DOM trickery.<br />
<span id="more-153"></span><br />
Playing the game is simple enough.  You use the four arrow keys and the Q/A key to pick one of six directions to go in.</p>
<p>At first, it can be very difficult to gauge the &#8220;3d&#8221; position of the apple.  Once you learn to use the shadows to your advantage, the game becomes pretty easy.  It&#8217;s still fun, though, and a great example of how Javascript can be used to make an interface for a game.</p>
<p>You see a few cool things here.</p>
<p>For one, the script captures keyboard input to drive the action.  Rather than clicking on buttons/boxes on the screen, the user can hit the appropriate keys and the game responds.  This is important for making a game feel &#8220;real&#8221; and not a kind of mash-up.</p>
<p>A lot of the positioning is done with &#8220;position: absolute&#8221; and the &#8220;top&#8221;/&#8221;left&#8221; styles.  This let&#8217;s you use the screen as a kind of canvas &#8211; drawing the images where-ever you need them.  It frees you from the constraints of normal HTML positioning, where you need to float/margin/padding stuff to get it to line up where you want.</p>
<p>It&#8217;s possible to read source code written in another language.  I just find this interesting.  The source is written in Spanish &#8211; so most of the variable/function names don&#8217;t mean a lot to me.  However, the code is pretty straightforward so it&#8217;s still easy enough to follow what&#8217;s going on.</p>
<p>Well go play some Worm.  This has inspired me to go do some coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/javascript-3d-worm/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript Strings: How to Do String Replacement in JS</title>
		<link>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/</link>
		<comments>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 20:55:31 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/</guid>
		<description><![CDATA[This morning I set about making a simple script to clean extra newline characters out of text. I could easily have processed the form contents in PHP, but I wanted to use Javascript to avoid reloading the page. The concept seemed simple enough, but I ran into a problem &#8211; I didn&#8217;t know how to [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I set about making a simple <a href="http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/">script to clean extra newline characters</a> out of text.  I could easily have processed the form contents in PHP, but I wanted to use Javascript to avoid reloading the page.</p>
<p>The concept seemed simple enough, but I ran into a problem &#8211; I didn&#8217;t know how to do string replacement in Javascript.  The first thing I thought was, &#8220;What is the Javascript equivalent to str_replace?&#8221;<br />
<span id="more-152"></span></p>
<h5>The JS str_replace &#8211; String.replace()</h5>
<p>As it turns out, the closest thing in Javascript to str_replace is the replace method of the String class.  It&#8217;s actually more akin to preg_replace &#8211; since it uses regular expressions &#8211; but it&#8217;s the closest thing I could find for a built-in string replacement function.</p>
<p>It&#8217;s implementation is pretty simple.  Create a new string object.  Then call the replace method with two parameters &#8211; a regular expression to match and a string to replace it with.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> hello <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> goodbye <span style="color: #339933;">=</span> hello.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/hello/i</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Goodbye'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example, we do a simple search for the word &#8216;hello&#8217; (the &#8216;i&#8217; signifies that the search is case insensitive).  When we find &#8216;hello,&#8217; we replace it with goodbye.</p>
<h5>Using Variable Values Inside Regex</h5>
<p>That&#8217;s simple enough.  However, what if you don&#8217;t want to hard-code the regular expression?</p>
<p>I was left wondering how I could use a variables value within the regular expression for the search.  If I placed the variable inside the / / boundaries of the regex, it would simply search for the name of the variable.</p>
<p>I eventually stumbled across the RegExp class &#8211; which incidentally makes it easier to build a regex expression.  You create an instance of the RegExp class by passing it two parameters &#8211; the string to match and any modifiers (&#8216;i&#8217; or &#8216;g&#8217;) that should be used for the search.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> hello <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> matchWord <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> match <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span>matchWord<span style="color: #339933;">,</span> <span style="color: #3366CC;">'i'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> goodbye <span style="color: #339933;">=</span> hello.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> <span style="color: #3366CC;">'Goodbye'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This has the same effect as the last example &#8211; except now the string to match comes from the variable matchWord.  With this, you could use some type of user input to determine what to match and replace.</p>
<h5>Recap &#8211; Replacing Strings in Javascript</h5>
<p>To recap&#8230;</p>
<ul>
<li>There is no direct equivalent to str_replace in Javascript.</li>
<li>Use String.replace &#8211; which is very similar to preg_replace.</li>
<li>Two parameters &#8211; a regex to match and a string to replace it with.</li>
<li>Build an instance of the RegExp class to use variable values in your regex.</li>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/22/javascript-str-replace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Text Cleaner &#8211; Remove Newlines from Text and Articles</title>
		<link>http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/</link>
		<comments>http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 19:12:43 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/</guid>
		<description><![CDATA[Do you have a bunch of old articles stored on your computer with hard linebreaks? These newline characters make it a real pain to upload your articles to your own site through a CMS or to article submission directories. They want free-flowing text that gets broken up automatically by the browser. If you need to [...]]]></description>
			<content:encoded><![CDATA[<p>Do you have a bunch of old articles stored on your computer with hard linebreaks?</p>
<p>These newline characters make it a real pain to upload your articles to your own site through a CMS or to article submission directories.  They want free-flowing text that gets broken up automatically by the browser.</p>
<p>If you need to get rid of these newline characters, use the Text Cleaner form below.  It works very simply.</p>
<p>Copy and paste your article with newline characters into the top textarea box.  Click the button.  Copy and paste the cleaned text out of the lower box.</p>
<p>Note:  The text cleaner preserves a sequence of &#8220;\n\n.&#8221;  It is assumed that these double newline characters are paragraph breaks, and removing them would make the article illegible.</p>
<hr />
<h4>Text Cleaner Tool: Remove Newline Characters</h4>
<form id=form-cleaner action="#" method="post" style="margin-top: 1em;">
<input type="button" id="submitClean" value="Clean Text" onClick="cleanText();"/></p>
<p>  <label for='dirty-text'>Enter the text to clean here.</label><br />
  <textarea id="dirty-text" name="text"></textarea></p>
<p>  <label for='clean-text'>The cleaned text will appear here.</label><br />
  <textarea id="clean-text" name="clean-text"></textarea><br />
</form>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/online-tools-design/text-cleaner-form/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Great Resources: Learning JS, AJAX, and DOM</title>
		<link>http://www.earn-web-cash.com/2008/02/19/resources-js-ajax-dom/</link>
		<comments>http://www.earn-web-cash.com/2008/02/19/resources-js-ajax-dom/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 23:00:50 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Online Tools]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/19/resources-js-ajax-dom/</guid>
		<description><![CDATA[Over the weekend, I decided it was finally time to learn how to use JS and AJAX. I&#8217;d deliberately avoided using JS for anything I could accomplish in PHP &#8211; but I admit that I was getting a bit intrigued by the AJAX hooplah. I found some very useful resources for dealing with JS, AJAX, [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend, I decided it was finally time to learn how to use JS and AJAX.  I&#8217;d deliberately avoided using JS for anything I could accomplish in PHP &#8211; but I admit that I was getting a bit intrigued by the AJAX hooplah.</p>
<p>I found some very useful resources for dealing with JS, AJAX, and DOM, so I thought I&#8217;d share them for any other eager learners.<br />
<span id="more-131"></span></p>
<h5>Getting Started with AJAX</h5>
<p>The first place I stopped in my quest for knowledge was the <a href="http://www.ibm.com/developerworks/views/web/libraryview.jsp?search_by=Mastering+Ajax">Mastering Ajax</a> series of articles at IBM&#8217;s Developer Works.</p>
<p>I read through the first half of the series.  Articles 1, 2, and 3 give a pretty good overview of how to create and use an AJAX request.  The later articles introduce the DOM and provide a good overview of how to work with the AJAX response and edit the existing web page.</p>
<p>After reading the first two articles, I was able to re-craft the examples and create some basic AJAX-driven forms of my own.  It wasn&#8217;t anything exciting &#8211; but the concept was easy enough to master.</p>
<p>Although you can get by without ever working directly with the AJAX request object, I agree with the author of this series of articles.  It&#8217;s best to learn to do something the hard way first &#8211; then you can take shortcuts.</p>
<h5>AJAX the Easy Way &#8211; Prototype.js</h5>
<p>Later in the weekend, I set about creating my first real AJAX application.  My <a href="http://www.earn-web-cash.com/2008/02/18/digg-badge-popular-posts/">Digg WordPress Plugin</a> uses AJAX to check the Digg API for info on an article after the page has loaded.</p>
<p>I tried to rework the code that I&#8217;d built with the Developer Works tutorial, but I ran into a problem when I had to make more than one request.  I knew I had to make extra instances of the HttpRequest object, but I was having trouble getting the code to execute the way I wanted it to.  Admittedly I&#8217;m a JS newbie &#8211; the first Javascript I ever wrote was the code from the first AJAX tutorial.</p>
<p>I found a great solution though &#8211; <a href="http://www.prototypejs.org/api">prototype.js</a>.  This is a javascript/ajax framework that makes it much simpler to work with the base AJAX class.</p>
<p>This made it much easier to create multiple instances of an AJAX request class and have them simultaneously fire off and handle requests.  It also handles some of the browser compatibility issues.  I like to do things on my own, but I&#8217;m pretty sure I&#8217;ll be using prototype.js from now on for any AJAX projects I work on.</p>
<h5>DOM: Document Object Model Resources</h5>
<p>Once I got a hang of sending and processing requests with AJAX, I had to turn to DOM to edit the page.  After reading through the Developer Works tutorial and fiddling a bit on my own, I found this surprisingly easy.</p>
<p>If you&#8217;ve got an understanding of OOP and XML, the DOM should be pretty straightforward for you.  However, there are a decent amount of methods/properties you&#8217;ll run into.  I found two great references that I&#8217;ll be using for a while until I get more familiar with the DOM methods/properties.</p>
<p>howtocreate.co.uk has a compact <a href="http://www.howtocreate.co.uk/tutorials/javascript/domstructure">list of pertinent methods and properties</a>.  It&#8217;s not explained very well, but it&#8217;s good for looking up a method quickly.  By contrast, <a href="http://www.javascriptkit.com/domref/elementmethods.shtml">Javascript Kit</a> has a thorough reference of methods and properties with explanations.  It&#8217;s a bit more verbose, but a better choice if you haven&#8217;t got a clue how a method works.</p>
<p>Working with JS, AJAX, and DOM is pretty simple &#8211; especially if you already know a scripting language like PHP.  So don&#8217;t wait any longer &#8211; go out and add to your repertoire.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/19/resources-js-ajax-dom/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

