<?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>Perplexed Labs &#187; JavaScript</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>jQuery 1.4.2 Released</title>
		<link>http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/</link>
		<comments>http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 17:56:56 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.perplexedlabs.com/?p=421</guid>
		<description><![CDATA[Just a quick note alerting everyone to the fact that jQuery has gotten EVEN EASIER AND FASTER. Go check out the release notes. Related posts:jQuery 1.4 Released jQuery 1.3.1 Go For Launch jQuery 1.4 Alpha 1 Released


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/' rel='bookmark' title='Permanent Link: jQuery 1.3.1 Go For Launch'>jQuery 1.3.1 Go For Launch</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Just a quick note alerting everyone to the fact that jQuery has gotten EVEN EASIER AND FASTER.</p>
<p>Go check out the <a href="http://blog.jquery.com/2010/02/19/jquery-142-released/">release notes</a>.</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/' rel='bookmark' title='Permanent Link: jQuery 1.3.1 Go For Launch'>jQuery 1.3.1 Go For Launch</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4 Released</title>
		<link>http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/</link>
		<comments>http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 20:57:24 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.perplexedlabs.com/?p=405</guid>
		<description><![CDATA[Many (awesome) changes http://blog.jquery.com/2010/01/14/jquery-14-released/. View the release notes here: http://jquery14.com/day-01/jquery-14 Related posts:jQuery 1.4.2 Released jQuery 1.4 Alpha 1 Released jQuery 1.4a2 (Alpha 2) Released


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4a2 (Alpha 2) Released'>jQuery 1.4a2 (Alpha 2) Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Many (awesome) changes <a href="http://blog.jquery.com/2010/01/14/jquery-14-released/">http://blog.jquery.com/2010/01/14/jquery-14-released/</a>.</p>
<p>View the release notes here: <a href="http://jquery14.com/day-01/jquery-14">http://jquery14.com/day-01/jquery-14</a></p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4a2 (Alpha 2) Released'>jQuery 1.4a2 (Alpha 2) Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4a2 (Alpha 2) Released</title>
		<link>http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/</link>
		<comments>http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 16:04:28 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[1.4]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery 1.4]]></category>

		<guid isPermaLink="false">http://blog.perplexedlabs.com/?p=400</guid>
		<description><![CDATA[The wonderful folks over at jQuery have released Alpha 2 of version 1.4 for all those testers out there. 1.4 is slated for official launch on January 14th - how cute :) Related posts:jQuery 1.4 Alpha 1 Released jQuery 1.3 Beta Testing jQuery 1.4.2 Released


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The wonderful folks over at jQuery have released <a href="http://blog.jquery.com/2009/12/18/jquery-14-alpha-2-released/">Alpha 2</a> of version 1.4 for all those testers out there.</p>
<p>1.4 is slated for official launch on January 14th - how cute :)</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4 Alpha 1 Released</title>
		<link>http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/</link>
		<comments>http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 16:57:23 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.perplexedlabs.com/?p=398</guid>
		<description><![CDATA[Just wanted to mention that jQuery 1.4 Alpha 1 has been released. Most of the changes seem to revolve around heavy optimization of some core functionality. Installing this alpha and testing in live applications will help get this release out! Related posts:jQuery 1.4a2 (Alpha 2) Released jQuery 1.4.2 Released jQuery 1.4 Released


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4a2 (Alpha 2) Released'>jQuery 1.4a2 (Alpha 2) Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Just wanted to mention that <a href="http://blog.jquery.com/2009/12/04/jquery-14-alpha-1-released/">jQuery 1.4 Alpha 1</a> has been released.</p>
<p>Most of the changes seem to revolve around heavy optimization of some core functionality.  Installing this alpha and testing in live applications will help get this release out!</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/12/19/jquery-1-4a2-alpha-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4a2 (Alpha 2) Released'>jQuery 1.4a2 (Alpha 2) Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP jQuery AJAX Javascript Long Polling</title>
		<link>http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/</link>
		<comments>http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/#comments</comments>
		<pubDate>Mon, 04 May 2009 14:00:14 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[long polling]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=217</guid>
		<description><![CDATA[Background "Long polling" is the name used to describe a technique which: An AJAX request is made (utilizing a javascript framework such as jQuery) The server waits for the data requested to be available, loops, and sleeps (your server-side PHP script) This loop repeats after data is returned to the client and processed (usually in [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>"Long polling" is the name used to describe a technique which:</p>
<ul>
<li>An AJAX request is made (utilizing a javascript framework such as jQuery)
<li>The server waits for the data requested to be available, loops, and sleeps (your server-side PHP script)
<li>This loop repeats after data is returned to the client and processed (usually in your AJAX request's onComplete callback function)
</ul>
<p>This essentially simulates a continuous real-time stream from the client to the server.  It can be more efficient than a regular polling technique because of the reduction in HTTP requests.  You're not asking over and over and over again for new data - you ask once and wait for an answer.  In most cases this reduces the latency in which data becomes available to your application.</p>
<p>There are a variety of use cases in which this technique can be handy.  At the top of the list are real-time web-based chat applications.  Each client executes a long polling loop for chat and user events (sign on/sign off/new message).  <a href="http://www.meebo.com">Meebo</a> is perhaps the greatest example of this.</p>
<p>It's important to note some of the server side technical limitations of long polling.  Because connections remain open for considerably longer time than a typical HTTP request/response cycle you want your web server to be able to handle a large number of simultaneous connections.  Apache isn't the best candidate for this type of situation.  <a href="http://nginx.net/">nginx</a> and <a href="http://www.lighttpd.net/">lighttpd</a> are two lightweight web servers built from the ground up to handle a high volume of simultaneous connections.  Both support the FastCGI interface and as such can be configured to support PHP.  Again, Meebo uses lighttpd.</p>
<p>For similar reasons - it's also a good idea to choose a different sub-domain to handle long polling traffic.  Because of client side browser limitations you don't want long polling connections interfering with regular HTTP traffic delivering page and media resources for your application.</p>
<h3>Implementation</h3>
<p><a href="http://www.jquery.com">jQuery</a> makes implementation a breeze.</p>
<pre class="brush: jscript;">
var lpOnComplete = function(response) {
	alert(response);
	// do more processing
	lpStart();
};

var lpStart = function() {
	$.post('/path/to/script', {}, lpOnComplete, 'json');
};

$(document).ready(lpStart);
</pre>
<p>Straightforward.  When the document is ready the loop begins.  Each iteration the returned data is processed and the loop is restarted.</p>
<p>On the server side - just like we discussed earlier:</p>
<pre class="brush: php;">
$time = time();
while((time() - $time) &lt; 30) {
	// query memcache, database, etc. for new data
	$data = $datasource-&gt;getLatest();

	// if we have new data return it
	if(!empty($data)) {
		echo json_encode($data);
		break;
	}

	usleep(25000);
}
</pre>
<p>Actually, a couple points of interest here.  We don't actually loop <em>infinitely</em> server side.  You may have noticed the logic for the while loop - if we've executed for more than 30 seconds we discontinue the loop and return nothing.  This nearly eliminates the possibility of substantial memory leaks.  Also, if we didn't put a cap on execution time we would need to print a "space" character and flush output buffers every iteration of the loop to keep PHP abreast to the status of this process/connection.  <strong>Without output being sent PHP cannot determine if the connection was lost via connection_status() or connection_aborted()</strong>.  As a result this could lead to a situation where there are an increasing number of "ghost" processes eating up server resources.  Not good!</p>
<p>That pretty much sums it up!  Not that difficult, right?</p>
<p>As always, questions/comments are welcome, hope this helps!</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>JavaScript roundTo Nearest Thousandth, Hundredth, Tenth, *</title>
		<link>http://blog.perplexedlabs.com/2009/02/06/javascript-roundto-nearest-thousandth-hundredth-tenth/</link>
		<comments>http://blog.perplexedlabs.com/2009/02/06/javascript-roundto-nearest-thousandth-hundredth-tenth/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 18:53:42 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[round]]></category>
		<category><![CDATA[rounding]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=153</guid>
		<description><![CDATA[This very simple function does exactly what the title suggests, it allows you to round to any specified accuracy. function roundTo(number, to) { return Math.round(number * to) / to; } alert(roundTo(1532, 100)); // 1500 alert(roundTo(26, 10)); // 30 If you want to get fancy, you could also modify the Number prototype for a more "integrated" [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/' rel='bookmark' title='Permanent Link: JavaScript Event Handler'>JavaScript Event Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/' rel='bookmark' title='Permanent Link: PHP jQuery AJAX Javascript Long Polling'>PHP jQuery AJAX Javascript Long Polling</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This very simple function does exactly what the title suggests, it allows you to round to any specified accuracy.</p>
<pre class="brush: jscript;">
function roundTo(number, to) {
	return Math.round(number * to) / to;
}

alert(roundTo(1532, 100)); // 1500
alert(roundTo(26, 10)); // 30
</pre>
<p>If you want to get fancy, you could also modify the Number prototype for a more "integrated" solution.</p>
<pre class="brush: jscript;">
Number.prototype.roundTo = function(to) {
	return Math.round(this * to) / to;
}
</pre>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/' rel='bookmark' title='Permanent Link: JavaScript Event Handler'>JavaScript Event Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/' rel='bookmark' title='Permanent Link: PHP jQuery AJAX Javascript Long Polling'>PHP jQuery AJAX Javascript Long Polling</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/02/06/javascript-roundto-nearest-thousandth-hundredth-tenth/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</title>
		<link>http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/</link>
		<comments>http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 20:21:34 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SCRIPT]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=136</guid>
		<description><![CDATA[For an internal project I've been working on I have to provide embeddable versions of internal tools for deployment on remote partner sites. I achieved this in my framework through, what I call, output format filters. Let's say you have an action "cloud" in a controller "markets". The URL to access this resource in its [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/' rel='bookmark' title='Permanent Link: PHP jQuery AJAX Javascript Long Polling'>PHP jQuery AJAX Javascript Long Polling</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/06/javascript-roundto-nearest-thousandth-hundredth-tenth/' rel='bookmark' title='Permanent Link: JavaScript roundTo Nearest Thousandth, Hundredth, Tenth, *'>JavaScript roundTo Nearest Thousandth, Hundredth, Tenth, *</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For an internal project I've been working on I have to provide embeddable versions of internal tools for deployment on remote partner sites.</p>
<p>I achieved this in my framework through, what I call, output format filters.  Let's say you have an action "cloud" in a controller "markets".  The URL to access this resource in its full glory would be:</p>
<blockquote>
<p>http://www.domainname.com/markets/cloud</p>
</blockquote>
<p>Pretty standard stuff.  Let's say we want to offer an embeddable version of this resource.  I can append an "output format filter" on this resource:</p>
<blockquote>
<p>http://www.domainname.com/markets/cloud/embed</p>
</blockquote>
<p>Alright, again, pretty standard stuff.  How on earth does this relate to dynamic javascript SCRIPT insertion?</p>
<p>On the remote partner site, I ask them to place the following HTML fragment in the desired location they want the resource to appear:</p>
<pre class="brush: xml;">
&lt;script language=&quot;javascript&quot; type=&quot;text/javscript&quot; src=&quot;http://www.domainname.com/markets/cloud/embed&quot;&gt;&lt;/script&gt;
</pre>
<p>One line of HTML.</p>
<p>How does this work under the hood?  It simply takes the view's content and prepares it as a valid javascript document.write.  It also specifies certain HTML headers, notably:</p>
<blockquote><p><strong>Content-Type: application/x-javascript</strong></p></blockquote>
<pre class="brush: php;">
function prepJS($js)
{
	// prepare the output as valid javascript
	$js = str_replace(&quot;\\&quot;, &quot;\\\\&quot;, $js);
	$js = preg_replace(&quot;/[\r\n]+/&quot;, '\n', $js);
	$js = str_replace('&quot;', '\&quot;', $js);
	$js = preg_replace(&quot;/&lt;script/i&quot;, '&lt;scr&quot;+&quot;ipt', $js);
	$js = preg_replace(&quot;/&lt;\/script/i&quot;, '&lt;/scr&quot;+&quot;ipt', $js);

	return $js;
}
</pre>
<pre class="brush: jscript;">
document.write(&quot;&lt;?php echo prepJS($viewContent); ?&gt;&quot;);
</pre>
<p>It should be fairly obvious what the function does.  It escapes double quotes, adds slashes, and replaces newlines and carriage returns with their inline equivalent.  Additionally and VERY important, it splits SCRIPT tag elements (if your view had inline SCRIPT's) into SCR"+"IPT so that the browser treats these as pure strings and doesn't cause JavaScript parsing errors.</p>
<p>What if the resource you are embedding depends upon the availability of certain JavaScript libraries or external source files?  The first response usually is, myself included, to simply document.write more SCRIPT tags!</p>
<pre class="brush: jscript;">
document.write(&quot;
&lt;?php prepJS('&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot; src=&quot;http://www.domainname.com/path/to/jquery.js&quot;&gt;&lt;/script&gt;'); ?&gt;
&quot;);
document.write(&quot;
&lt;?php prepJS('&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot; src=&quot;http://www.domainname.com/path/to/morejs.js&quot;&gt;&lt;/script&gt;'); ?&gt;
&quot;);
document.write(&quot;&lt;?php echo prepJS($viewContent); ?&gt;&quot;);
</pre>
<p>The problem is of course that in IE, <a href="http://stackoverflow.com/questions/94141/javascripts-document-write-inline-script-execution-order">there's no telling what order those SCRIPT tags will get executed</a>.  Furthermore, if your resource depends on them, they need to be executed <strong>before</strong> you document.write the content.</p>
<p>My solution to this problem was two-fold.  For other reasons not worth going into here, it's generally a good idea to concatenate all your JavaScript source files into one larger file, minify (if desired), cache, and serve the result.  Taking advantage of this we can reduce the above to a single additional SCRIPT insertion.  Tack this snippet onto the end of that file:</p>
<pre class="brush: jscript;">
if(typeof(embedReady) != 'undefined') {
	embedReady();
}
</pre>
<p>And we come up with this:</p>
<pre class="brush: jscript;">
document.write(&quot;
&lt;?php prepJS('&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot; src=&quot;http://www.domainname.com/path/to/cached_combined_js.js&quot;&gt;&lt;/script&gt;'); ?&gt;
&quot;);
var embedReady = function() {
    document.write(&quot;&lt;?php echo prepJS($viewContent); ?&gt;&quot;);
}
</pre>
<p>Now we're only concerned with a single SCRIPT load.  We've safely placed our document.write of content inside a function that's called at the end of our dependency SCRIPT.  In an elegant, browser agnostic fashion our resource will have it's dependencies loaded before it executes.</p>
<p>Yay?</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/' rel='bookmark' title='Permanent Link: PHP jQuery AJAX Javascript Long Polling'>PHP jQuery AJAX Javascript Long Polling</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/06/javascript-roundto-nearest-thousandth-hundredth-tenth/' rel='bookmark' title='Permanent Link: JavaScript roundTo Nearest Thousandth, Hundredth, Tenth, *'>JavaScript roundTo Nearest Thousandth, Hundredth, Tenth, *</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.3.1 Go For Launch</title>
		<link>http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/</link>
		<comments>http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 18:05:32 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=128</guid>
		<description><![CDATA[Just a quick note. I waited for the 1.3.1 release of jQuery to make sure all major bugs had been resolved (brand new selector engine). This weekend I decided to go for the upgrade. Flawless. Even the plugins all worked as expected. Fantastic job. Related posts:jQuery 1.3 Beta Testing jQuery 1.4.2 Released jQuery 1.4 Alpha [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Just a quick note.</p>
<p>I waited for the <a href="http://blog.jquery.com/2009/01/21/jquery-131-released/">1.3.1 release of jQuery</a> to make sure all major bugs had been resolved (brand new selector engine).  This weekend I decided to go for the upgrade.  Flawless.  Even the plugins all worked as expected.</p>
<p>Fantastic job.</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/12/05/jquery-1-4-alpha-1-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Alpha 1 Released'>jQuery 1.4 Alpha 1 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.3 Beta Testing</title>
		<link>http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/</link>
		<comments>http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 18:11:17 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=118</guid>
		<description><![CDATA[The official jQuery blog has announced the availability of jQuery 1.3b1. They're asking for testers to iron out remaining bugs - looking to release on January 14th. Major changes include: Selector Engine - The selector code has undergone a complete rewrite - it’s likely that some edge cases still exist here. DOM Manipulation (append/prepend/before/after) - [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/16/converting-from-prototype-to-jquery/' rel='bookmark' title='Permanent Link: Converting from Prototype to jQuery'>Converting from Prototype to jQuery</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/' rel='bookmark' title='Permanent Link: jQuery 1.3.1 Go For Launch'>jQuery 1.3.1 Go For Launch</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blog.jquery.com/2008/12/22/help-test-jquery-13-beta-1/" target="_blank">official jQuery blog</a> has announced the availability of jQuery 1.3b1.  They're asking for testers to iron out remaining bugs - looking to release on January 14th.</p>
<p>Major changes include:</p>
<ul>
<li>Selector Engine - The selector code has undergone a complete rewrite - it’s likely that some edge cases still exist here.</li>
<li>DOM Manipulation (append/prepend/before/after) - This code has also undergone a large rewrite along with some of the logic for executing inline script elements.</li>
<li>.offset() - Another method that has been completely rewritten.</li>
<li>Event Namespaces - The logic for handling namespaced events has been completely rewritten.</li>
<li>Event Triggering - When triggering an event the event now bubbles up the DOM - this is likely to cause some problems.</li>
</ul>
<p>Go lend a hand!</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/11/16/converting-from-prototype-to-jquery/' rel='bookmark' title='Permanent Link: Converting from Prototype to jQuery'>Converting from Prototype to jQuery</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/01/24/jquery-131-go-for-launch/' rel='bookmark' title='Permanent Link: jQuery 1.3.1 Go For Launch'>jQuery 1.3.1 Go For Launch</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/20/jquery-1-4-2-released/' rel='bookmark' title='Permanent Link: jQuery 1.4.2 Released'>jQuery 1.4.2 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting from Prototype to jQuery</title>
		<link>http://blog.perplexedlabs.com/2008/11/16/converting-from-prototype-to-jquery/</link>
		<comments>http://blog.perplexedlabs.com/2008/11/16/converting-from-prototype-to-jquery/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 02:52:47 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=68</guid>
		<description><![CDATA[Let me start off by saying I absolutely love Prototype. When a friend showed me how much it helped with writing cross-browser compatible code, I was an instant fan. Over the years of being a Prototype user I paid close attention to the many posts about why X is a better framework than Y. I [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/' rel='bookmark' title='Permanent Link: JavaScript Event Handler'>JavaScript Event Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Let me start off by saying I absolutely love Prototype.  When a friend showed me how much it helped with writing cross-browser compatible code, I was an instant fan.</p>
<p>Over the years of being a Prototype user I paid close attention to the many posts about why X is a better framework than Y.  I had always planned on at least TRYING another framework at some point to see what all the fuss was about.</p>
<p>Initially what attracted me to jQuery was it's compactness.  It doesn't have separately defined classes for each component, like Prototype.  There's one point of entry and most everything revolves around DOM elements.  There's no distinction between selecting a single or multiple DOM element(s) and, because jQuery only uses a selector syntax, you don't get confused whether the parameter passed is an ID, class, etc.</p>
<p>Furthermore, I never really liked having to use $ for single selection and $$ for multiple selections in Prototype.  Relatively speaking, with jQuery I find that performing actions on multiple DOM elements is easier.  </p>
<p>Chaining is really where jQuery shines.  It reminds me of using the 'with' construct in Visual Basic back in the day.  "This is the object that I want to perform actions on... do this, that, and this... thanks!".  </p>
<p>However, unlike Prototype where one could test for the existence of an object by:</p>
<pre class="brush: jscript;">
if($('objectID')) {
// do something
}
</pre>
<p>In jQuery it isn't quite so intuitive.  Since all jQuery instances are collections of elements, you test for the existence of an element by checking its length property:</p>
<pre class="brush: jscript;">
if(jQuery('#objectId').length == 1) {
// do something
}
</pre>
<p>In jQuery, binding to events is also very intuitive.  Combined with the power of chaining adding event handling to multiple objects is ridiculously simple.  And, because the code executed in your event callback has the context of the object that triggered the event, you can (more easily than bindAsEventListener in Prototype) just access the object within the callback via 'this'.</p>
<p>This brings me to one issue that did initially cause some conversion conflict.  Prototype does a good job of automatically extending standard objects, objects returned, and parameters passed.  With jQuery, you have to get used to 'get()'ing the root DOM element if you need to manipulate it and likewise, extending a root DOM element to a full fledged jQuery instance (such as 'jQuery(this)') if you want to manipulate it via jQuery.</p>
<p>Another primary reason why I switched to jQuery is because it has built in support for basic animations.  Transitional animations give that extra something to a web app.  Having to hand-code them isn't pleasant.  Obviously Prototype has scriptaculous, but thats a huge size (and therefor performance) commitment to make for just a simple fade, no?</p>
<p>jQuery was built to be extensible.  It seems to me that Prototype intended to be the "be all, end all" solution to your JavaScript woes.  jQuery takes a more sensible approach - it provides the fundamental structure and handles the most-desired scenarios.  If there's something it doesn't do out of the box there's probably a plugin available that does.  Most likely someone has come across the same shortcoming already and written a plugin to fulfill the need.</p>
<p>Some plugins I ended up needing immediately were:</p>
<ul>
<li>Date picker</li>
<li>CSS Extraction from Ajax responses (IE issue)</li>
<li>Ancestry (to mimic Prototype's descendantOf())</li>
<li>DOM element creation (to mimic the functionality of Prototype's Element class)</li>
</ul>
<p>I prefer to create DOM elements in JavaScript programatically without using HTML.  jQuery doesn't have a built in solution for creating DOM elements like Prototype does with its Element class.  Fortunately, Lukasz Rajchel has written the DOMEC (DOM Element Creation) plugin.  It's lightweight and is a drop-in replacement for those familiar with the syntax of Prototype's Element.</p>
<p>During the conversion process I found it to be most helpful to load jQuery side by side with Prototype using jQuery's no-conflict functionality.  As I slowly combed through my JavaScript removing Prototype dependencies I would periodically remove Prototype to see if and where I missed any outliers.  This also requires you to use the jQuery() syntax as opposed to the traditional $() syntax.  In doing so this helps easily identify where Prototype code remains.</p>
<p>How have your conversions to jQuery gone?  Have you converted and never looked back?  Your thoughts are appreciated...</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/' rel='bookmark' title='Permanent Link: JavaScript Event Handler'>JavaScript Event Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/01/14/jquery-1-4-released/' rel='bookmark' title='Permanent Link: jQuery 1.4 Released'>jQuery 1.4 Released</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2008/11/16/converting-from-prototype-to-jquery/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
