<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Reading GET variables with JavaScript</title>
	<atom:link href="http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/</link>
	<description>a blog about building stuff on the web</description>
	<lastBuildDate>Mon, 09 Jan 2012 18:55:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: JRO</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4569</link>
		<dc:creator>JRO</dc:creator>
		<pubDate>Wed, 14 Dec 2011 06:00:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-4569</guid>
		<description>This works better: 
 
 
    function getQueryVariable(variable) { 
        var query = window.location.search.substring(1); 
        var vars = query.split(&quot;&amp;&quot;); 
        for (var i = 0; i &lt; vars.length; i++) { 
            var pair = vars[i].split(&quot;=&quot;); 
            if (pair[0] == variable) { 
                return unescape(pair[1]); 
            } 
        } 
        return false; 
    } 
 </description>
		<content:encoded><![CDATA[<p>This works better: </p>
<p>    function getQueryVariable(variable) {<br />
        var query = window.location.search.substring(1);<br />
        var vars = query.split(&quot;&amp;&quot;);<br />
        for (var i = 0; i &lt; vars.length; i++) {<br />
            var pair = vars[i].split(&quot;=&quot;);<br />
            if (pair[0] == variable) {<br />
                return unescape(pair[1]);<br />
            }<br />
        }<br />
        return false;<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jerel</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-4565</link>
		<dc:creator>jerel</dc:creator>
		<pubDate>Tue, 13 Dec 2011 21:34:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-4565</guid>
		<description>not to lurk, been trying to get this to do anything for hours now. Before i grind my teeth into dust, could someone be so kind as to show any of this working in context? 
 
say like &lt;a href=&quot;http://www.whatever.com/index.php?var1=hi&quot; rel=&quot;nofollow&quot;&gt;www.whatever.com/index.php?var1=hi&lt;/a&gt; is the url 
then the page w the script outputs var1 and prints hi? 
 
I know php well, new to js, getting a tad crazy over here... </description>
		<content:encoded><![CDATA[<p>not to lurk, been trying to get this to do anything for hours now. Before i grind my teeth into dust, could someone be so kind as to show any of this working in context? </p>
<p>say like <a href="http://www.whatever.com/index.php?var1=hi" rel="nofollow">http://www.whatever.com/index.php?var1=hi</a> is the url<br />
then the page w the script outputs var1 and prints hi? </p>
<p>I know php well, new to js, getting a tad crazy over here&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tomasz</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3083</link>
		<dc:creator>tomasz</dc:creator>
		<pubDate>Thu, 12 May 2011 07:24:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-3083</guid>
		<description>Don&#039;t you prefer? 
s = s &#124;&#124; window.location.search; 
over 
s = s ? s : window.location.search;  
 </description>
		<content:encoded><![CDATA[<p>Don&#039;t you prefer?<br />
s = s || window.location.search;<br />
over<br />
s = s ? s : window.location.search;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3060</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 30 Apr 2011 15:18:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-3060</guid>
		<description>Yep, it doesn&#039;t work without escaping the question mark. 
Maybe backslashes are autoremoved from the code section as well </description>
		<content:encoded><![CDATA[<p>Yep, it doesn&#039;t work without escaping the question mark.<br />
Maybe backslashes are autoremoved from the code section as well</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marty</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-3072</link>
		<dc:creator>Marty</dc:creator>
		<pubDate>Thu, 28 Apr 2011 01:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-3072</guid>
		<description>You&#039;re a god!!! hehehehe, you found the solution, you escaped the question mark &#039;cause it is a reserved word of regular expressions!! </description>
		<content:encoded><![CDATA[<p>You&#039;re a god!!! hehehehe, you found the solution, you escaped the question mark &#039;cause it is a reserved word of regular expressions!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: @Zakoholic</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-2934</link>
		<dc:creator>@Zakoholic</dc:creator>
		<pubDate>Wed, 09 Feb 2011 00:22:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-2934</guid>
		<description>I think I can answer this myself because it seems like the backslash isn&#039;t shown in the comments section (not sure about your initial post). The line of code in my initial comment should read: 
 
return (s=s.replace(/^BACKSLASH?/,&#039;&amp;&#039;)...  
 
With &#039;BACKSLASH&#039; being a real backslash. </description>
		<content:encoded><![CDATA[<p>I think I can answer this myself because it seems like the backslash isn&#039;t shown in the comments section (not sure about your initial post). The line of code in my initial comment should read: </p>
<p>return (s=s.replace(/^BACKSLASH?/,&#039;&amp;&#039;)&#8230;  </p>
<p>With &#039;BACKSLASH&#039; being a real backslash.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: @Zakoholic</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-2933</link>
		<dc:creator>@Zakoholic</dc:creator>
		<pubDate>Tue, 08 Feb 2011 21:43:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-2933</guid>
		<description>Thanks for this helpful function! One thing I&#039;d like to mention is, that I got the same error message as the FettesPS but when I tried his suggestion of replacing the slashes with single quotes it wasn&#039;t working (probably because &#039;^?&#039; indicates it&#039;s a string). So I tried to escape the question mark with a backslash and now that the line looks like: 
 
return (s=s.replace(/^?/,&#039;&amp;&#039;)... 
 
it&#039;s working as it should. As I&#039;m a JS newbie I&#039;d like to know why I had to escape the ?, when it was apparently working fine for you (and Kip) without doing so. </description>
		<content:encoded><![CDATA[<p>Thanks for this helpful function! One thing I&#039;d like to mention is, that I got the same error message as the FettesPS but when I tried his suggestion of replacing the slashes with single quotes it wasn&#039;t working (probably because &#039;^?&#039; indicates it&#039;s a string). So I tried to escape the question mark with a backslash and now that the line looks like: </p>
<p>return (s=s.replace(/^?/,&#039;&amp;&#039;)&#8230; </p>
<p>it&#039;s working as it should. As I&#039;m a JS newbie I&#039;d like to know why I had to escape the ?, when it was apparently working fine for you (and Kip) without doing so.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FettesPS</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-2803</link>
		<dc:creator>FettesPS</dc:creator>
		<pubDate>Thu, 03 Feb 2011 01:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-2803</guid>
		<description>Was getting &quot;invalid identifier ?&quot;  as an error and it pointed to &quot;s.replace(/^?/,&#039;&amp;&#039;)&quot; 
 
Had to replace the /&#039;s with single quotes, making the line look like: 
 
s.replace(&#039;^?&#039;,&#039;&amp;&#039;) 
 
After that it worked great! </description>
		<content:encoded><![CDATA[<p>Was getting &quot;invalid identifier ?&quot;  as an error and it pointed to &quot;s.replace(/^?/,&#039;&amp;&#039;)&quot; </p>
<p>Had to replace the /&#039;s with single quotes, making the line look like: </p>
<p>s.replace(&#039;^?&#039;,&#039;&amp;&#039;) </p>
<p>After that it worked great!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Fraser</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-2046</link>
		<dc:creator>Josh Fraser</dc:creator>
		<pubDate>Fri, 14 Jan 2011 21:00:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-2046</guid>
		<description>I like it.  Updated my code and added attribution for you as well. </description>
		<content:encoded><![CDATA[<p>I like it.  Updated my code and added attribution for you as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kip</title>
		<link>http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/comment-page-1/#comment-2045</link>
		<dc:creator>kip</dc:creator>
		<pubDate>Fri, 14 Jan 2011 20:44:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.onlineaspect.com/?p=291#comment-2045</guid>
		<description>One other change I&#039;d recommend: differentiate between parameter that is defined but empty string and a parameter that isn&#039;t defined at all.  To do this you&#039;d have to change the regex and the return line: 
 
  function $_GET(q,s) { 
    s = s ? s : window.location.search; 
    var re = new RegExp(&#039;&amp;&#039;+q+&#039;(?:=([^&amp;]*))?(?=&amp;&#124;$)&#039;,&#039;i&#039;); 
    return (s=s.replace(/^?/,&#039;&amp;&#039;).match(re)) ? (typeof s[1] == &#039;undefined&#039; ? &#039;&#039; : decodeURIComponent(s[1])) : undefined; 
  } 
 
Or to make it a little more verbose, if desired: 
 
  function $_GET(q,s) { 
    s = s ? s : window.location.search; 
    var re = new RegExp(&#039;&amp;&#039;+q+&#039;(?:=([^&amp;]*))?(?=&amp;&#124;$)&#039;,&#039;i&#039;); 
    s = s.replace(/^?/,&#039;&amp;&#039;).match(re); 
    if(s) 
      return typeof s[1] != &#039;undefined&#039; ? decodeURIComponent(s[1]) : &#039;&#039;; 
  } 
 
Examples: 
$_GET(&#039;abc&#039;, &#039;?abc=123&amp;def=456&#039;)   =&gt; Returns &#039;123&#039; 
$_GET(&#039;abc&#039;, &#039;?abc=&amp;def=456&#039;)   =&gt; Returns &#039;&#039; 
$_GET(&#039;abc&#039;, &#039;?abc&amp;def=456&#039;)   =&gt; Returns &#039;&#039; 
$_GET(&#039;abc&#039;, &#039;?def=456&#039;)   =&gt; In your code, returns &#039;&#039;. With my modification, returns undefined. </description>
		<content:encoded><![CDATA[<p>One other change I&#039;d recommend: differentiate between parameter that is defined but empty string and a parameter that isn&#039;t defined at all.  To do this you&#039;d have to change the regex and the return line: </p>
<p>  function $_GET(q,s) {<br />
    s = s ? s : window.location.search;<br />
    var re = new RegExp(&#039;&amp;&#039;+q+&#039;(?:=([^&amp;]*))?(?=&amp;|$)&#039;,&#039;i&#039;);<br />
    return (s=s.replace(/^?/,&#039;&amp;&#039;).match(re)) ? (typeof s[1] == &#039;undefined&#039; ? &#039;&#039; : decodeURIComponent(s[1])) : undefined;<br />
  } </p>
<p>Or to make it a little more verbose, if desired: </p>
<p>  function $_GET(q,s) {<br />
    s = s ? s : window.location.search;<br />
    var re = new RegExp(&#039;&amp;&#039;+q+&#039;(?:=([^&amp;]*))?(?=&amp;|$)&#039;,&#039;i&#039;);<br />
    s = s.replace(/^?/,&#039;&amp;&#039;).match(re);<br />
    if(s)<br />
      return typeof s[1] != &#039;undefined&#039; ? decodeURIComponent(s[1]) : &#039;&#039;;<br />
  } </p>
<p>Examples:<br />
$_GET(&#039;abc&#039;, &#039;?abc=123&amp;def=456&#039;)   =&gt; Returns &#039;123&#039;<br />
$_GET(&#039;abc&#039;, &#039;?abc=&amp;def=456&#039;)   =&gt; Returns &#039;&#039;<br />
$_GET(&#039;abc&#039;, &#039;?abc&amp;def=456&#039;)   =&gt; Returns &#039;&#039;<br />
$_GET(&#039;abc&#039;, &#039;?def=456&#039;)   =&gt; In your code, returns &#039;&#039;. With my modification, returns undefined.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

