<?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; ajax</title>
	<atom:link href="http://www.post-hipster.com/category/ajax/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>
		<item>
		<title>Stupid IE6 and its iframe cookie idiocy needs a throat punch</title>
		<link>http://www.post-hipster.com/2008/07/02/stupid-ie6-and-its-iframe-cookie-idiocy-needs-a-throat-punch/</link>
		<comments>http://www.post-hipster.com/2008/07/02/stupid-ie6-and-its-iframe-cookie-idiocy-needs-a-throat-punch/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 13:08:41 +0000</pubDate>
		<dc:creator>chip</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[302]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[throat punch]]></category>

		<guid isPermaLink="false">http://www.post-hipster.com/?p=43</guid>
		<description><![CDATA[&#8230;so let&#8217;s say you have a site, that has two versions, a.throat-punch.com and b.throat-punch.com. And Apache uses a cookie to determine which version you should be viewing, and sends a 302 redirect if you&#8217;re on the wrong domain. Now let&#8217;s say you want to access a page on this site from an &#60;iframe&#62; from an [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" src="http://farm1.static.flickr.com/7/10200825_fcb4805688.jpg?v=0" alt="F--- you, IE 6" width="319" height="310" />&#8230;so let&#8217;s say you have a site, that has two versions, a.throat-punch.com and b.throat-punch.com. And Apache uses a cookie to determine which version you should be viewing, and sends a 302 redirect if you&#8217;re on the wrong domain.</p>
<p>Now let&#8217;s say you want to access a page on this site from an &lt;iframe&gt; from an external domain, say www.whowantsathroatpunch.com. Stupid IE6 <strong>will not send the right cookies in the request headers</strong>. In fact, I&#8217;m pretty sure it will send no cookies. Why? Because it&#8217;s a dick. Every Other Browser does this correctly. It&#8217;s not a security issue -it&#8217;s not like you&#8217;re asking IE6 to send cookies that belong to another domain, or to teach our nation&#8217;s children to read or anything. You&#8217;re asking the browser for the cookies that have been previously set, and IE6 in its infinite dick-kickery is failing in that basic respect.</p>
<p>What really gets my goat is that, whereas the iframe will not get its cookies, if you make the src of the iframe do an AJAX request to another page on the site, <em>that</em> request will get its cookies sent correctly.</p>
<p>The workaround I found is to give the &lt;iframe&gt; src a new page if the browser is IE6, and on that new page, make an AJAX request to another new page that outputs the value of the cookie you&#8217;re looking for. When you receive the AJAX request, you can then parse the response and redircet the user to the now-corrected original &lt;iframe&gt; src. It&#8217;s stupid and inefficient, I know:</p>
<pre>&lt;script type="text/javascript"&gt;
	// note: this page only called from IE6, so no browser testing
	// or compatibility checks are needed. I'm rocking the prototype.js for
	// the ajax, you do what you like.

	function editionTest() {
		var test_url = '/cookie_tester.php';
		var domain = new Ajax.Request(test_url, {
		    method:'get',
    		onSuccess: function(transport){
				var response = transport.responseText;
				if (response.search('site_a') &gt; -1) {
					redirector('a');
				} else if (response.search('site_b') &gt; -1) {
					redirector('b');
				} else {
					redirector('');
				}
    		},
			onFailure: function() {
				redirector('');
			}
		  });
	}

	function redirector(domain) {
		var url = parseUrl();
		var id = url['id'];
		var var = url['var'];
		var domain_parsed = 'http://' + domain + '.throat-punch.com/page_you_really_wanted.php?id=' + id + '&amp;var1=' + var1;
		window.location = domain_parsed;
	}

	function parseUrl() {
		var hash = {};
		var url = String(document.location).split('?');
		if (url[1]) {
			var vars= url[1].split('&amp;');
			var ct = vars.length;
			for (var i=0; i&lt;ct; i++) {
				var item = vars[i].split('=');
				var name = item[0];
				var value = item[1];
				hash[name] = value;
			}
		}
		return hash;
	}

Event.observe(window, 'load', function() {
	editionTest();
});
&lt;/script&gt;</pre>
<p>I&#8217;m writing this post solely for google to pick it up, just in case anyone ever is in this position again. So, dude who googled &#8220;ie6 cookies iframe 302 throat punch&#8221;, this one&#8217;s for you.</p>
<p>(thanks to <a href="http://flickr.com/photos/celebdu/10200825/" target="_blank">celebdu</a> for the photo)</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=Stupid%20IE6%20and%20its%20iframe%20cookie%20idiocy%20needs%20a%20throat%20punch&amp;linkurl=http%3A%2F%2Fwww.post-hipster.com%2F2008%2F07%2F02%2Fstupid-ie6-and-its-iframe-cookie-idiocy-needs-a-throat-punch%2F">Share/Save</a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.post-hipster.com/2008/07/02/stupid-ie6-and-its-iframe-cookie-idiocy-needs-a-throat-punch/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Elegant little PHP JSON encoder</title>
		<link>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/</link>
		<comments>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 14:18:52 +0000</pubDate>
		<dc:creator>chip</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.post-hipster.com/?p=35</guid>
		<description><![CDATA[update: Jay Williams has flushed this out to be a lot more useful. Check out his rendition here. &#8230;so I wrote (what I think is a) clever little function (well, two functions) to take a complex php variable and turn it into a json-ized string, ready to be passed back to javascript. It works on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>update: <a title="Jay's Pictures" href="http://photoblog.dview.us/" target="_blank">Jay Williams</a> has flushed this out to be a lot more useful. Check out his rendition <a title="New Hotness" href="http://pastie.textmate.org/398110" target="_blank">here</a>.</strong></p>
<p>&#8230;so I wrote (what I think is a) clever little function (well, two functions) to take a complex php variable and turn it into a json-ized string, ready to be passed back to javascript. It works on the principle that json really only has a couple rules if text formatting: strings go inside double quotes, with both slashes and double-quotes escaped, iterative arrays are comma delimited inside brackets [obj1,obj2], and associative arrays go inside curly-brackets {key:val,key:val}. Using these three points, and a little bit (ok, a lot of) recursion, and voila, an elegant little function.</p>
<p>Of course, this doesn&#8217;t handle unicode, or any really special cases, but if you&#8217;re looking for a basic object parser, and you dont&#8217; have access to php 5.2, which has it built into the language, it&#8217;ll do the trick&#8230;</p>
<pre>/**
 * input an object, returns a json-ized string of said object
 * @return
 * @param $obj Object
 */
function php_json_encode($obj) {
	if (is_array($obj)) {
		if (array_is_associative($obj)) {
			$arr_out = array();
			foreach ($obj as $key=&gt;$val) {
				$arr_out[] = '"' . $key . '":' . php_json_encode($val);
			}
			return '{' . implode(',', $arr_out) . '}';
		} else {
			$arr_out = array();
			$ct = count($obj);
			for ($j = 0; $j &lt; $ct; $j++) {
				$arr_out[] = php_json_encode($obj[$j]);
			}
			return '[' . implode(',', $arr_out) . ']';
		}
	} else {
		if (is_int($obj)) {
			return $obj;
		} else {
			$str_out = stripslashes(trim($obj));
			$str_out = str_replace(array('"', '', '/'), array('\"', '\', '/'), $str_out);
			return '"' . $str_out . '"';
		}

	}
}

function array_is_associative($array) {
	$count = count($array);
	for ($i = 0; $i &lt; $count; $i++) {
		if (!array_key_exists($i, $array)) {
			return true;
		}
	}
	return false;
}</pre>
<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=Elegant%20little%20PHP%20JSON%20encoder&amp;linkurl=http%3A%2F%2Fwww.post-hipster.com%2F2008%2F02%2F15%2Felegant-little-php-json-encoder%2F">Share/Save</a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
