<?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; query</title>
	<atom:link href="http://www.earn-web-cash.com/tag/query/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>
	</channel>
</rss>

