<?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>Barryvan &#187; Javascript</title>
	<atom:link href="http://www.barryvan.com.au/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryvan.com.au</link>
	<description>Music, Programming, Design</description>
	<lastBuildDate>Wed, 23 Jun 2010 04:18:22 +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>Generative Music</title>
		<link>http://www.barryvan.com.au/2010/03/generative-music/</link>
		<comments>http://www.barryvan.com.au/2010/03/generative-music/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 08:08:11 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Ambient]]></category>
		<category><![CDATA[Chillout]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=443</guid>
		<description><![CDATA[Continuing my HTML5 and canvas experiments, I&#8217;ve put together a generative music system. Essentially, a series of particles move across a field, occasionally triggering sounds &#8212; the sound triggered depends on their location in the field. There is, of course, a little bit more to it than that. Under the hood, I&#8217;ve got a series [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my HTML5 and canvas experiments, I&#8217;ve put together a generative music system. Essentially, a series of particles move across a field, occasionally triggering sounds &#8212; the sound triggered depends on their location in the field.</p>
<p>There is, of course, a little bit more to it than that. Under the hood, I&#8217;ve got a series of HTML5 Audio objects that are used to provide polyphonic audio using a simple round-robin algorithm (I encoded the audio in OGG, so you&#8217;ll need to use an OGG-friendly browser, like <a href="http://www.getfirefox.com">Firefox</a>). The particles are much simpler than those in my <a href="/2010/03/canvas-swarms/">previous canvas dalliance</a>, in that they don&#8217;t swarm, and their motion is more linear.</p>
<div class="screenshots">
<a href="/demos/generative.html"><br/><br />
<img alt="Generative Audio Screenshot" src="/wp-content/uploads/2010/03/generative.png" class="aligncenter size-full"/><br/><br />
View (and hear!) it live<br/><br />
</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/03/generative-music/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Canvas Swarms</title>
		<link>http://www.barryvan.com.au/2010/03/canvas-swarms/</link>
		<comments>http://www.barryvan.com.au/2010/03/canvas-swarms/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 00:27:18 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Processing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=440</guid>
		<description><![CDATA[I&#8217;ve been meaning to start playing with the HTML5 &#60;canvas&#62; element for a while now, and yesterday I took the opportunity. I translated a Processing sketch I made a while ago into JavaScript (with a few minor enhancements). View it live Essentially, 1 to 3 swarms of particles move around the canvas, reproducing when the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to start playing with the HTML5 &lt;canvas&gt; element for a while now, and yesterday I took the opportunity. I translated a Processing sketch I made a while ago into JavaScript (with a few minor enhancements).</p>
<div class="screenshots">
<a href="/demos/swarms/swarms.html"><br />
<img class="aligncenter size-full" src="/demos/swarms/swarms.png" alt="Swarms Screenshot"/><br />
View it live<br />
</a>
</div>
<p>Essentially, 1 to 3 swarms of particles move around the canvas, reproducing when the conditions are just right, and dying of old age. Quite simple, but the patterns produced can be really quite pretty.</p>
<p>One interesting thing that I discovered whilst doing this is that you can&#8217;t pass around a canvas&#8217; context at the instantiation of a MooTools class &#8212; it complains about wrapped natives. That&#8217;s why, if you like in the source JavaScript, you&#8217;ll see me pass the actual context around to various functions. I&#8217;d be interested in hearing if anyone has a workaround for this, because this is, well, a bit clunky.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/03/canvas-swarms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HTML: IE file submission</title>
		<link>http://www.barryvan.com.au/2010/02/html-ie-file-submission/</link>
		<comments>http://www.barryvan.com.au/2010/02/html-ie-file-submission/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 07:57:22 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=437</guid>
		<description><![CDATA[I&#8217;ve been bumping up against an interesting bug in Internet Explorer recently, and, having just found the solution, thought I&#8217;d share it with you. The problem is that in Internet Explorer (tested 7 &#038; 8), when your document is in quirks mode, uploading a file sometimes just sends through the file name, without the actual [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been bumping up against an interesting bug in Internet Explorer recently, and, having just found the solution, thought I&#8217;d share it with you.</p>
<p>The problem is that in Internet Explorer (tested 7 &#038; 8), when your document is in quirks mode, uploading a file sometimes just sends through the file name, without the actual body of the request. Put the document into standards mode, and it all works. It should be noted that this is when you&#8217;re dynamically setting up the elements used with JavaScript.</p>
<p>The cause? In quirks mode, the <em>enctype</em> attribute isn&#8217;t supported. So whilst setting &#8220;encType&#8221; on the form element to the correct &#8220;multipart/form-data&#8221; will indeed set this attribute, it won&#8217;t actually cause the upload to include the file. Instead, you need to set the <em>encoding</em> attribute to this value, too. It certainly doesn&#8217;t help that the MSDN article on the &lt;input type=&#8221;file&#8221;&gt; element tells you to set &#8220;enctype&#8221;, but makes no mention of &#8220;encoding&#8221;.</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ms533744(VS.85).aspx">MSDN: &#8216;Encoding&#8217; property</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/cc304100%28VS.85%29.aspx">MSDN: &#8216;Enctype&#8217; property</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/ms535263%28VS.85%29.aspx">MSDN: &lt;input type=&#8221;file&#8221;&gt; element</a></li>
</ul>
<p>I couldn&#8217;t find any reference to this problem on the intertubes (although maybe I just didn&#8217;t look hard enough), so hopefully this will help someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/02/html-ie-file-submission/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript: Print a single element</title>
		<link>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/</link>
		<comments>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 08:18:47 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=434</guid>
		<description><![CDATA[Sometimes, you&#8217;ll want to allow users the ability to print only a part of your page; for example, a table but not the various links around the page. It&#8217;s possible to use a printing stylesheet, but this can cause severe headaches when you need different parts printed at different times. Really, we want to be [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;ll want to allow users the ability to print only a part of your page; for example, a table but not the various links around the page. It&#8217;s possible to use a printing stylesheet, but this can cause severe headaches when you need different parts printed at different times. Really, we want to be able to just say <strong>element.printElement()</strong>, and have it just work. That&#8217;s what the MooTools function below does. It&#8217;s loosely based around the concepts outlined at <a href="http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm">this website</a>.</p>
<pre class="brush: javascript">Element.implement({
	printElement: function() {
		var strName = &#039;printer-&#039; + (new Date()).getTime(),
		styles = $$(&#039;link[type=text/css]&#039;).clone(),
		title = document.title,
		that = this,
		iframe = new IFrame({
			name: strName,
			styles: {
				width: 1,
				height: 1,
				position: &#039;absolute&#039;,
				left: -9999
			},
			events: {
				load: function() {
					var doc = this.contentDocument || window.frames[strName].document;
					doc.title = title;
					$(doc.body).adopt(styles, that.clone());
					this.contentWindow.focus(); // IE requires us to focus before printing, or the parent prints.
					this.contentWindow.print();
				}
			}
		}).inject($(document.body));
		iframe.dispose.delay(15000); // Destroy the iframe in 15s so that it doesn&#039;t hang around.
	}
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2010/02/javascript-print-a-single-element/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Limiting the contents of a string via RegEx</title>
		<link>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/</link>
		<comments>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 08:10:49 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=332</guid>
		<description><![CDATA[Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex you supply.</p>
<pre class="brush: javascript">String.implement({
	limitContent: function(allowedRegex) {
		return $splat(this.match(allowedRegex)).join(&#039;&#039;);
	}
});</pre>
<p>Basically, the function takes the result of evaluating the regular expression on the string, converts it into an array if it isn&#8217;t one, and then joins the array&#8217;s elements together with an empty string.</p>
<h2>Examples:</h2>
<pre class="brush: javascript">console.log(&quot;12345&quot;.limitContent(/.{4}/)); // Only allow four characters
console.log(&quot;joe@mail.com&quot;.limitContent(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/)); // Only allow email addresses
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Wave &amp; iPhone</title>
		<link>http://www.barryvan.com.au/2009/11/google-wave-iphone/</link>
		<comments>http://www.barryvan.com.au/2009/11/google-wave-iphone/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 06:10:54 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[wave]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=301</guid>
		<description><![CDATA[I just tried out Google Wave on the iPhone. Interestingly, despite an initial &#8220;your browser is not supported&#8221; message, the actual system sports a rather snazzy app-like interface on the iPhone. I&#8217;ll be interested to see what kind of support Wave will &#8216;officially&#8217; have on mobile platforms, and perhaps even more interested in what &#8216;unofficial&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>I just tried out Google Wave on the iPhone. Interestingly, despite an initial &#8220;your browser is not supported&#8221; message, the actual system sports a rather snazzy app-like interface on the iPhone.</p>
<p>I&#8217;ll be interested to see what kind of support Wave will &#8216;officially&#8217; have on mobile platforms, and perhaps even more interested in what &#8216;unofficial&#8217; support there&#8217;ll be. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/11/google-wave-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MooTools object messaging</title>
		<link>http://www.barryvan.com.au/2009/10/mootools-object-messaging/</link>
		<comments>http://www.barryvan.com.au/2009/10/mootools-object-messaging/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 08:19:08 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=280</guid>
		<description><![CDATA[Events In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks: You can&#8217;t attach events to non-existent objects. Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when [...]]]></description>
			<content:encoded><![CDATA[<h2>Events</h2>
<p>In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks:</p>
<h3>You can&#8217;t attach events to non-existent objects.</h3>
<p>Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when object B exists.</p>
<h3>If an object starts to listen for an event after it&#8217;s already fired, it&#8217;ll never hear it.</h3>
<p>Because content on the web isn&#8217;t always delivered in perfect order (especially when you&#8217;re loading scripts synchronously), it&#8217;s possible for an object to try to listen for an event after it&#8217;s already been fired. Obviously, this means that your listener object will never run the code that&#8217;s dependent on that event, which could be Bad Thing™.</p>
<h3>It&#8217;s not particularly easy to know which object is listening for which events.</h3>
<p>There are ways around this, but you can&#8217;t just dir() the listeners in Firebug.</p>
<h2>A messaging system</h2>
<p>For all of these reasons (and probably a few more that I&#8217;ve forgotten about), a messaging system can be an invaluable addition to your arsenal when writing JavaScript. How does a messaging system work? Well, interested objects &#8216;register&#8217; themselves as listeners for particular message &#8216;handles&#8217;, and other objects can send messages using those &#8216;handles&#8217;. Below is a very simple MooTools messaging system that I knocked up, which has a few cool features, including:</p>
<ul>
<li>When you register() a listener, you can have its callback immediately fire if that message has ever been sent before.
<li>
<li>You can very easily see which callbacks are associated with which messages by simply dir()-ing the &#8216;listeners&#8217; member.</li>
<li>You can unregister() a listener at any time (provided you&#8217;ve got a reference to the function and the handle).</li>
<li>Handles can be any valid JavaScript type &#8212; Strings, Numbers, even Objects.</li>
</ul>
<p>Feel free to use and extend this system &#8212; as I mentioned, this is a <strong>very</strong> simple system. If you do extend it, let me know in the comments!</p>
<p>Let me know what you think about this system in the comments.</p>
<pre class="brush: javascript">
barryvan.base.Messaging = new Class({
	listeners: $H(),
	sentMessages: [],

	initialize: function() {

	},

	/**
	 * Register a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to listen for.
	 * callback [Function]: The function to be called when the handle is sent a message. The contents of the messages will be included in the function call.
	 * dontCheck [Boolean]: If falsey and the handle has had a message sent to it, immediately call the callback function (without contents), and continue to add the listener as normal.
	 */
	register: function(handle, callback, dontCheck) {
		if ($type(callback) !== &#039;function&#039;) return;

		if (!dontCheck &amp;&amp; this.sentMessages[handle]) {
			callback();
		}

		if (!this.listeners.has(handle)) this.listeners[handle] = [];
		this.listeners[handle].push(callback);
	},

	/**
	 * Unregister a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to cease listening for.
	 * callback [Function]: The function which was earlier assigned as the callback for the messages.
	 */
	unregister: function(handle, callback) {
		if (this.listeners.has(handle)) {
			this.listeners[handle].erase(callback);
		}
	},

	/**
	 * Send a message to the given handle with the given contents -- send the contents to all the registered listeners for that handle.
	 * handle [String]: The message &#039;handle&#039; to transmit to.
	 * contents [Mixed]: The contents to be sent to the listeners.
	 */
	send: function(handle, contents) {
		this.sentMessages.include(handle);
		if (this.listeners.has(handle)) {
			this.listeners[handle].each(function(callback) {
				callback(contents);
			});
		}
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/10/mootools-object-messaging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quicksort an array of objects</title>
		<link>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/</link>
		<comments>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 07:14:51 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=225</guid>
		<description><![CDATA[Often, you will need to sort an array of objects in Javascript. The inbuilt sort() function can&#8217;t do this, but here is a Quicksort implementation for doing just this. Parameters array The array to be sorted. (See below for an implementation on the Array Native itself, which makes this variable unnecessary). key The key to [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to sort an array of objects in Javascript. The inbuilt <em>sort()</em> function can&#8217;t do this, but here is a Quicksort implementation for doing just this.</p>
<h2>Parameters</h2>
<p><strong>array</strong> The array to be sorted. (See below for an implementation on the Array Native itself, which makes this variable unnecessary).</p>
<p><strong>key</strong> The key to sort by. Make sure every object in your array has this key.</p>
<h2>Examples</h2>
<pre class="brush: javascript">
var objs = [
	{fruit:&quot;cherry&quot;},
	{fruit:&quot;apple&quot;},
	{fruit:&quot;banana&quot;}
];

console.log(objs.sortObjects(&#039;fruit&#039;));
// Logs [{fruit:&quot;apple&quot;},{fruit:&quot;banana&quot;},{fruit:&quot;cherry&quot;}] to the console
</pre>
<h2>The code</h2>
<pre class="brush: javascript">
sortObjects: function(array, key) {
	for (var i = 0; i &lt; array.length; i++) {
		var currVal = array[i][key];
		var currElem = array[i];
		var j = i - 1;
		while ((j &gt;= 0) &amp;&amp; (array[j][key] &gt; currVal)) {
			array[j + 1] = array[j];
			j--;
		}
		array[j + 1] = currElem;
	}
}
</pre>
<h3>Implemented on the Array native:</h3>
<pre class="brush: javascript">
Array.implement({
	sortObjects: function(key) {
		for (var i = 0; i &lt; this.length; i++) {
			var currVal = this[i][key];
			var currElem = this[i];
			var j = i - 1;
			while ((j &gt;= 0) &amp;&amp; (this[j][key] &gt; currVal)) {
				this[j + 1] = this[j];
				j--;
			}
			this[j + 1] = currElem;
		}
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/08/quicksort-an-array-of-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript string ellipsising</title>
		<link>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/</link>
		<comments>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 07:03:17 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[string manipulation]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=217</guid>
		<description><![CDATA[Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you. This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters: toLength (required) [...]]]></description>
			<content:encoded><![CDATA[<p>Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you.</p>
<p>This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters:</p>
<p><strong>toLength</strong> (required) The number of characters to truncate the string to (or 0 to disable ellipsising)</p>
<p><strong>where</strong> (optional, default &#8216;end&#8217;) A string representing where the ellipsis should be placed &#8212; &#8216;front&#8217;, &#8216;middle&#8217;, or &#8216;end&#8217;</p>
<p><strong>ellipsis</strong> (option, default &#8216;\u2026&#8242;) A string to be used as the ellipsis.</p>
<h2>Examples</h2>
<pre class="brush: javascript">
// Our clichéd string
var s = &#039;Jackdaws love my great big sphinx of quartz&#039;;

alert(s.ellipsise(10));
// Alerts &quot;Jackdaws l…&quot;

alert(s.ellipsise(10, &#039;front&#039;));
// Alerts &quot;… of quartz&quot;

alert(s.ellipsise(10, &#039;middle&#039;, &#039;pony&#039;));
// Alerts &quot;Jackdponyuartz&quot;</pre>
<h2>The code</h2>
<pre class="brush: javascript">String.implement({
	ellipsise: function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
		if (toLength &lt; 1) return this;
		ellipsis = ellipsis || &#039;\u2026&#039;;
		if (this.length &lt; toLength) return this;
		switch (where) {
			case &#039;front&#039;:
				return ellipsis + this.substr(this.length - toLength);
				break;
			case &#039;middle&#039;:
				return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
				break;
			case &#039;end&#039;:
			default:
				return this.substr(0, toLength) + ellipsis;
				break;
		}
	}
});</pre>
<p>If you&#8217;re not using MooTools, you can use this variant instead:</p>
<pre class="brush: javascript">String.prototype.ellipsise = function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
	if (toLength &lt; 1) return this;
	ellipsis = ellipsis || &#039;\u2026&#039;;
	if (this.length &lt; toLength) return this;
	switch (where) {
		case &#039;front&#039;:
			return ellipsis + this.substr(this.length - toLength);
			break;
		case &#039;middle&#039;:
			return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
			break;
		case &#039;end&#039;:
		default:
			return this.substr(0, toLength) + ellipsis;
			break;
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoid Javascript&#8217;s &#8216;with&#8217; keyword</title>
		<link>http://www.barryvan.com.au/2009/05/avoid-javascripts-with-keyword/</link>
		<comments>http://www.barryvan.com.au/2009/05/avoid-javascripts-with-keyword/#comments</comments>
		<pubDate>Tue, 12 May 2009 00:06:10 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=162</guid>
		<description><![CDATA[Javascript is a fantastic language &#8212; in fact, it&#8217;s become the language that I do most of my programming in nowadays. It&#8217;s flexible, fast, and powerful. Unfortunately, though, it suffers from a few flaws, which, although not critical, can be frustrating. One of the potentially most confusing features is the with keyword, which promises a [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript is a fantastic language &#8212; in fact, it&#8217;s become the language that I do most of my programming in nowadays. It&#8217;s flexible, fast, and powerful. Unfortunately, though, it suffers from a few flaws, which, although not critical, can be frustrating. One of the potentially most confusing features is the <em>with</em> keyword, which promises a lot, but can really just make life difficult.</p>
<p>The <em>with</em> keyword might appear to be harmless enough: it allows you to avoid typing long references; instead of</p>
<pre class="brush: javascript">ah.woom.ba.weh.lyric = &#039;In the jungle&#039;;</pre>
<p>we can type</p>
<pre class="brush: javascript">with (ah.woom.ba.weh) {
  lyric = &#039;In the jungle&#039;;
}</pre>
<p>But what happens if we happen to have a global variable named <em>lyric</em>? In the example below, which <em>lyric</em> should be modified?</p>
<pre class="brush: javascript">lyric = &#039;In the jungle&#039;;
with (ah.woom.ba.weh) {
  lyric = &#039;The mighty jungle&#039;;
}</pre>
<p>The simplest way to deal with this issue is to use a variable:</p>
<pre class="brush: javascript">var a = ah.woom.ba.weh;
a.lyric = &#039;The mighty jungle&#039;;</pre>
<p>Now there is no ambiguity.</p>
<p><em>Based on a <a href="http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/">post by Douglas Crockford</a> at the YUI Blog.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/05/avoid-javascripts-with-keyword/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
