<?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>www.post-hipster.com &#187; javascript</title>
	<atom:link href="http://www.post-hipster.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.post-hipster.com</link>
	<description>A Shady Lane, everybody wants one</description>
	<lastBuildDate>Wed, 30 Dec 2009 10:47:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Be Careful parsing your URL string with Javascript</title>
		<link>http://www.post-hipster.com/2009/02/18/be-careful-parsing-your-url-string-with-javascript/</link>
		<comments>http://www.post-hipster.com/2009/02/18/be-careful-parsing-your-url-string-with-javascript/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 02:33:33 +0000</pubDate>
		<dc:creator>chip</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[hackery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.post-hipster.com/?p=105</guid>
		<description><![CDATA[&#8230;so I know you&#8217;ve looked around the net for a good method to parse URL variables with javascript, and I&#8217;m sure that you&#8217;ve found some elegant method that looks something like: function parseLocation() { var turl = window.location.search; ar = turl.split('&#38;'); var b = {}; for (var i = 0; i &#60; ar.length; i++) { [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;so I know you&#8217;ve looked around the net for a good method to parse URL variables with javascript, and I&#8217;m sure that you&#8217;ve found some elegant method that looks something like:</p>
<pre>function parseLocation() {
	var turl = window.location.search;
	ar = turl.split('&amp;');
	var b = {};

	for (var i = 0; i &lt; ar.length; i++) {
		var c = ar[i].split('=');
		b[c[0]] = c[1];
	}
	return b;
}</pre>
<p>and you&#8217;ve thought, &#8220;oh, look how nice and compact that is. I shall use it post-haste!&#8221; Well, let me, as Mr. Guy Who&#8217;s Gotten Plenty Of Grumpy Complaints About This, light a candle rather than curse your darkness.</p>
<p>It is a good method, don&#8217;t get me wrong. It works great in theory. The problem is, say you copy/paste you url into, say, your Blogging Software of choice*. It might then re-interpret your url from:</p>
<pre>http://post-hipster.com/parsey.html?var1=hey&amp;var2=mambo&amp;var3=fagabefe</pre>
<p>into</p>
<pre>http://post-hipster.com/parsey.html?var1=hey&amp;amp;var2=mambo&amp;amp;var3=fagabefe</pre>
<p>and, on some browsers*, no amount of encode() or decode() will make that go away, which, in essence, changes your parsed variable names int &#8220;amp;var2&#8243; and &#8220;amp;var3&#8243;</p>
<p>My solution, however inelegant, is to make the function explicitly look for the variables in question:</p>
<pre>function parseLocation() {
	var turl = window.location.search;
	ar = turl.split('&amp;');
	var b = {var1:'default_for_var1', var2:''};

	for (var i = 0; i &lt; ar.length; i++) {
		var c = ar[i].split('=');
		if (c[0].indexOf('var1') &gt; -1) {
			b['var1'] = c[1];
		} else if (c[0].indexOf('var2') &gt; -1) {
			b['var2'] = c[1];
		} else if (c[0].indexOf('var3') &gt; -1) {
			b['var3'] = c[1];
		}
	}
	return b;
}</pre>
<p>I know, I coulda just RegEx&#8217;ed the &#8216;amp;&#8217; away, but this way prevents any other encoding mishaps. If this was a global function, across a buncha different pages, I probably would just look for and get rid of the &#8216;amp;&#8217;</p>
<p>So there you go. Unfortunately, URL parsing isn&#8217;t always as nice as we want it  to be.</p>
<p>* unfortunately I can&#8217;t be more specific than this, because I haven&#8217;t yet been able to duplicate the error on any browser / os combination I&#8217;ve tried, but I&#8217;ve gotten enough complaints about it to know that it exists.</p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=www.post-hipster.com&amp;siteurl=http%3A%2F%2Fwww.post-hipster.com%2F&amp;linkname=Be%20Careful%20parsing%20your%20URL%20string%20with%20Javascript&amp;linkurl=http%3A%2F%2Fwww.post-hipster.com%2F2009%2F02%2F18%2Fbe-careful-parsing-your-url-string-with-javascript%2F">Share/Save</a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.post-hipster.com/2009/02/18/be-careful-parsing-your-url-string-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
