<?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>Ryan Bosinger &#187; Javascript</title>
	<atom:link href="http://ryanbosinger.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryanbosinger.com/blog</link>
	<description>things I learn, as I learn them</description>
	<lastBuildDate>Thu, 19 Aug 2010 16:59:11 +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>Javascript: Get variables from querystring</title>
		<link>http://ryanbosinger.com/blog/2008/javascript-get-variables-from-querystring/</link>
		<comments>http://ryanbosinger.com/blog/2008/javascript-get-variables-from-querystring/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 17:31:55 +0000</pubDate>
		<dc:creator>rbosinger</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://ryanbosinger.com/blog/?p=40</guid>
		<description><![CDATA[


        
            
                Problem:
            
       [...]]]></description>
			<content:encoded><![CDATA[<a href='http://ryanbosinger.com/blog/wp-content/uploads/2008/08/screenhunter_02-aug-06-1651.gif'><img src="http://ryanbosinger.com/blog/wp-content/uploads/2008/08/screenhunter_02-aug-06-1651.gif" alt="" title="Querystring" width="375" height="156" class="aligncenter size-full wp-image-41" /></a>

<div class="problem-solution">
        <p class="problem">
            <strong>
                Problem:
            </strong>
        </p>
        <blockquote>
            You need to grab some information from the querystring of a URL using only javascript.
        </blockquote>
       
        <p class="solution">
            <strong>
                Solution:
            </strong>
        </p>
        <blockquote>
            Use some code I found!
        </blockquote>
    </div>
<span id="more-40"></span>
<p>I found this code:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/</span>
<span style="color: #003366; font-weight: bold;">function</span> Querystring<span style="color: #009900;">&#40;</span>qs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// optionally pass a querystring to parse</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">params</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #339933;">=</span>Querystring_get
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>qs <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		qs<span style="color: #339933;">=</span>location.<span style="color: #660066;">search</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>location.<span style="color: #660066;">search</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>qs.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Turn &lt;plus&gt; back to &lt;space&gt;</span>
<span style="color: #006600; font-style: italic;">// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1</span>
	qs <span style="color: #339933;">=</span> qs.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\+/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span>
	<span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> qs.<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: #006600; font-style: italic;">// parse out name/value pairs separated via &amp;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// split out each name=value pair</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>args.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> value<span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<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: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>pair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pair.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>pair<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: #000066; font-weight: bold;">else</span>
			value <span style="color: #339933;">=</span> <span style="color: #000066;">name</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">params</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Querystring_get<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> default_<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// This silly looking line changes UNDEFINED to NULL</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>default_ <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> default_ <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> value<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">params</span><span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">=</span>default_<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> value
<span style="color: #009900;">&#125;</span></pre></div></div>




<p>
So throw that in your page, and then call that sucker like this:
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Parse the current page's querystring</span>
<span style="color: #003366; font-weight: bold;">var</span> qs <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Querystring<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>




<p>
And then just go right on ahead and populate some variables with some query string variables:
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> v1 <span style="color: #339933;">=</span> qs2.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name1&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #003366; font-weight: bold;">var</span> v3 <span style="color: #339933;">=</span> qs2.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name3&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;default value&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>



<div class="google_ads_01">
<script type="text/javascript"><!--
google_ad_client = "pub-1962502741893893";
/* 468x60, created 5/27/08 */
google_ad_slot = "8820555819";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<p>
And use that information for whatever you needed it for in the first place.  Is nice?
</p>]]></content:encoded>
			<wfw:commentRss>http://ryanbosinger.com/blog/2008/javascript-get-variables-from-querystring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
