<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Comments on snippet: 'Get Url Parameters'</title>
<link>http://snipplr.com</link>
<description>Snipplr comments feed'</description>
<language>en-us</language>
<pubDate>Thu, 23 May 2013 23:30:38 GMT</pubDate>
<item>
<title>Kobee1203 said on 7/21/12</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ function getUrlParams() {
        var map = {};
        var parts = window.location.href.replace(/[?&amp;]+([^=&amp;]+)=([^&amp;#]*)/gi, function(m,key,value) {
        map[key] = (value === undefined) ? '' : value; // value.substring(1);
    });
    return map;
    }

I modified the regex expression to ignore the href tag. Just adding '`#`' the group for the parameter value. ]]></description>
<pubDate>Sat, 21 Jul 2012 02:01:34 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>Reikooters said on 4/6/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ Yes, the "map" variable is not accessible outside the scope of the function. As it states in the first line, the function returns the variable.

You would use this function, for example:

var stuff = getUrlVars();

and then access the data using stuff['foo']

This does allow for you to do stuff like:

var stuff = getUrlVars();
var stuff2 = getUrlVars();

and have 2 separate variables, each having an associative array of the GET parameters. You may only need to get and use them one time for what you're doing, but either way this allows for you to do it more than once, and be able to name the variable whatever you want. This is just good coding practice imo. ]]></description>
<pubDate>Wed, 06 Apr 2011 14:35:13 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>agdm said on 2/25/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ My update to this, more concisely explained:  
http://snipplr.com/view/49625/mapping-of-url-parameters/ ]]></description>
<pubDate>Fri, 25 Feb 2011 05:38:36 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>agdm said on 2/25/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ the properties of "map" are not accessible outside the scope of the function. When I move the declaration for the object outside of the function, its properties are accessible.

// moved outside the function
var map = {};

function getUrlVars() {
	var parts = window.location.href.replace(/[?&amp;]+([^=&amp;]+)=([^&amp;]*)/gi, function(m,key,value) {
		map[key] = value;
	});
	return map;
}

// call the function
getUrlVars();

// view url parameters
console.log(map.param1);
console.log(map.param2);
console.log(map.param3); ]]></description>
<pubDate>Fri, 25 Feb 2011 05:15:24 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>lazfsh said on 2/3/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ Couldn't get that to work. Maybe there is something else to it. ]]></description>
<pubDate>Thu, 03 Feb 2011 03:59:21 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>lazfsh said on 2/2/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ FIgured it out:

    for (var key in map)
    {
        window[key] = map[key];
    }
untested. Thanks [SO](http://stackoverflow.com/posts/4866504/edit) ]]></description>
<pubDate>Wed, 02 Feb 2011 05:59:33 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>lazfsh said on 1/29/11</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ Is there a way to modify this script, like maybe with a for loop, to change each variable so that
    foo=map['foo']
basically I want to use the variables in the rest of my code without typing the map['foo'] part. ]]></description>
<pubDate>Sat, 29 Jan 2011 06:45:29 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>nathanbrauer said on 6/29/10</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ (it was better spaced/indented before I hit submit) ]]></description>
<pubDate>Tue, 29 Jun 2010 19:30:57 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>nathanbrauer said on 6/29/10</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ `function getUrlVars() {
	var map = {};
	var parts = window.location.search.replace(/[?&amp;]+([^=&amp;]+)(=[^&amp;]*)?/gi, function(m,key,value) {
		map[key] = (value === undefined) ? true : value.substring(1);
	});
	return map;
}`

Here's an improvement on this code.  Now it will allow for valueless keys. Any keys which are present but have no value will be set to true. ]]></description>
<pubDate>Tue, 29 Jun 2010 19:30:26 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
<item>
<title>aoit said on 2/15/10</title>
<link>http://snipplr.com/view/19838/get-url-parameters/</link>
<description><![CDATA[ I have posted this code to my site, but can't see the data passed to the page. I have tried using a 
document.write (map['a']);

and alert (a)

using the variable a and b in the url with data of john and jones respectively.


I have put these lines of code in the function, at the end, and in a separate script, but can't get the data displayed on the page or on an alert.

Thank you for the help. ]]></description>
<pubDate>Mon, 15 Feb 2010 20:00:58 GMT</pubDate>
<guid>http://snipplr.com/view/19838/get-url-parameters/</guid>
</item>
</channel>
</rss>