<?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>Position Absolute, resources for the web developer &#187; Cedric Dugas</title>
	<atom:link href="http://www.position-absolute.com/author/karnius/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.position-absolute.com</link>
	<description>Get your web worker news fix</description>
	<lastBuildDate>Wed, 28 Jul 2010 19:44:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to the Google WebFont Loader and how to avoid @font-face text flickering with it</title>
		<link>http://www.position-absolute.com/articles/introduction-to-the-google-webfont-loader-and-how-to-avoid-font-face-text-flickering-with-it/</link>
		<comments>http://www.position-absolute.com/articles/introduction-to-the-google-webfont-loader-and-how-to-avoid-font-face-text-flickering-with-it/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 01:24:42 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[CSS / HTML]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>
		<category><![CDATA[featured]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=3027</guid>
		<description><![CDATA[<p>You remember the &#8220;old days&#8221; when you were cursing at sIFR because it would not give you the font-size you wanted, did text flickering on page load or was just not working at all on IE? Well those days are&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>You remember the &#8220;old days&#8221; when you were cursing at sIFR because it would not give you the font-size you wanted, did text flickering on page load or was just not working at all on IE? Well those days are all gone, the introduction of Cufon sometime ago solved most of the sIFR issues, but one of the big downside was that you could not select the text anymore. </p>
<p>Now with <a target="_blank" href="http://craigmod.com/journal/font-face/">@font-face</a> we finally got a native way of using custom fonts in our websites. Unfortunately this is not as easy as it should be. This is probably why Google launched recently the <a target="_blank" href="http://code.google.com/apis/webfonts/docs/webfont_loader.html">Google WebFont Loader</a> and the <a target="_blank" href="http://code.google.com/webfonts">Google font directory</a>. Two easy way of using @font-face without any trouble, well, less trouble.</p>
<h2>The text flickering problem all over again</h2>
<p>@font-face suffers of the same illness that its predecessors, until the font file is loaded it will display your text with the fallback font family. When you know that 20px with an hand written font can be a lot smaller than 20px with Arial, this becomes a major problem, you don&#8217;t want to see an enormous text until your custom font is loaded.</p>
<h2>Google WebFont Loader at the rescue</h2>
<p>I will be showing how you can use the google font loader to avoid text flickering and have a good fallback font in case the font file is not loading. You get the best of all world, but it comes with a price of a light javascript hack.</p>
<p><strong>Getting started</strong></p>
<p>Using the basic google font ability is easy (but it is quite a bunch of lines of code), you just embed the font you want!</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp;&lt;script type=&quot;text/javascript&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; WebFontConfig = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; google: { families: [ &#39;Tangerine&#39;, &#39;Cantarell&#39; ] }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; (function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; var wf = document.createElement(&#39;script&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; wf.src = (&#39;https:&#39; == document.location.protocol ? &#39;https&#39; : &#39;http&#39;) +
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; wf.type = &#39;text/javascript&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; wf.async = &#39;true&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; var s = document.getElementsByTagName(&#39;script&#39;)[0];
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; s.parentNode.insertBefore(wf, s);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; })();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &lt;/script&gt;</div>
</li>
</ol>
</div>
<p>As you can see, in this example you just need to change this line  <em>google: { families: [ 'Tangerine', 'Cantarell' ] }</em> for the font you want from the <a target="_blank" href="http://code.google.com/webfonts">Google Font Directory</a> (other fonts are also available like those from typekit.com). There is also quite a bunch of options you can use, have a look at the <a target="_blank" href="http://code.google.com/apis/webfonts/docs/webfont_loader.html">loader API</a> for more information.</p>
<p>Now this script does one thing interesting, it adds a css class to your document html, it will add <strong>wf-loading</strong> until the font is loaded,  <strong>wf-inactive</strong> if it can&#8217;t load the font, and  <strong>wf-active</strong> when it&#8217;s successful. We can use this at our advantage.  You can provide a valid CSS fallback that protect yourself from enormous and actually define your fallback font instead of being dependent of the same line-height and font-size.</p>
<h2>An example</h2>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;head&gt; &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;script type=&quot;text/javascript&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;WebFontConfig = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;google: { families: [ &#39;Tangerine&#39;, &#39;Reenie Beanie&#39; ] }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;};
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var wf = document.createElement(&#39;script&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.src = (&#39;https:&#39; == document.location.protocol ? &#39;https&#39; : &#39;http&#39;) +
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.type = &#39;text/javascript&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.async = &#39;true&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var s = document.getElementsByTagName(&#39;script&#39;)[0];
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.parentNode.insertBefore(wf, s);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;})();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;&lt;style type=&quot;text/css&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: arial;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size:20px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line-height:22px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 22px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-loading p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visibility:hidden;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-loading h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visibility:hidden;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-inactive p, .wf-inactive h1{ visibility:visible;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-active p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: &#39;Tangerine&#39;, serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size:33px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line-height:26px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-active h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: &#39;Reenie Beanie&#39;, serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 33px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&lt;/style&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;/head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;h1&gt;This is a title with a custom font&lt;/h1&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;p&gt;No text flickering and got the somewhat the same font height in fallback&lt;/p&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;p&gt;View the source to have a look at the code&lt;/p&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;/body&gt;</div>
</li>
</ol>
</div>
<p class="clearfix"><a class="demo" href="http://www.position-absolute.com/creation/font/example1.html" target="_blank">View demo</a></p>
<p>In this example we can see that we have a very good font-size fallback we use wf-active to set our custom font, and we just do our normal css flow as a fallback.</p>
<h2>A flicker you say?</h2>
<p>The text still flicker a bit at random times, specially in IE, this is because, wf-loading is added when the javascript file from Google is parsed in the document, and for some reasons (I guess it is a bug), even is we put async false, sometimes the wf-loading class is added to late.</p>
<p>The best way to avoid the flicker behavior is to add a class <em>wf-loading</em> to the html document in javascript ourself, you then add the rule <em>visibility:hidden</em> in css to it, and on <em> .wf-inactive and .wf-active</em> you simply add <em>visibility:visible</em>, the same css rules added in the last example. This way if the font can&#8217;t be loaded we will receive the wf-inactive and the text will be displayed. </p>
<p>With this solution your users with javascript disabled will still see your text with your normal fonts.<br />
<em><br />
<strong>(This solution has been updated, Thanks to <a href="http://paulirish.com/">Paul Irish</a> idea in the comments section)</em></strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; document.getElementsByTagName(&quot;html&quot;)[0].setAttribute(&quot;class&quot;,&quot;wf-loading&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; // &nbsp;NEEDED to push the wf-loading class to your head
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; document.getElementsByTagName(&quot;html&quot;)[0].setAttribute(&quot;className&quot;,&quot;wf-loading&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // for IE&#8230;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var wf = document.createElement(&#39;script&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.src = (&#39;https:&#39; == document.location.protocol ? &#39;https&#39; : &#39;http&#39;) +
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#39;://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.type = &#39;text/javascript&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wf.async = &#39;false&#39;;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var s = document.getElementsByTagName(&#39;script&#39;)[0];
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.parentNode.insertBefore(wf, s);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;})();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &lt;style type=&quot;text/css&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: arial;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size:20px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 22px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-loading p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visibility:hidden;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-loading h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visibility:hidden;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-inactive p, .wf-inactive h1{ visibility:visible;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-active p {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: &#39;Tangerine&#39;, serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size:33px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visibility:visible;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;.wf-active h1 {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: &#39;Reenie Beanie&#39;, serif;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-size: 33px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; visibility:visible;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;&lt;/style&gt;</div>
</li>
</ol>
</div>
<p class="clearfix"><a class="demo" href="http://www.position-absolute.com/creation/font/" target="_blank">View demo</a></p>
<h2>That&#8217;s it</h2>
<p>Hope it will help you guys get your custom fonts working as you want!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/introduction-to-the-google-webfont-loader-and-how-to-avoid-font-face-text-flickering-with-it/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Optimizing javascript/jQuery loading time, a beginner&#8217;s guide</title>
		<link>http://www.position-absolute.com/articles/optimizing-javascriptjquery-loading-time-a-beginners-guide/</link>
		<comments>http://www.position-absolute.com/articles/optimizing-javascriptjquery-loading-time-a-beginners-guide/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 13:31:12 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2961</guid>
		<description><![CDATA[<p>So you created that nice website or web application and would like to go a step further in optimizing the front-end loading time? With some great free tools you can easily optimize your javascript to load 2 or 3 times&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>So you created that nice website or web application and would like to go a step further in optimizing the front-end loading time? With some great free tools you can easily optimize your javascript to load 2 or 3 times faster if you are willing to trade the regular way of embedding javascript file.</p>
<h2>Loading script asynchronously</h2>
<p>One thing you need to know is that your script tag block the rendering of your page. In fact, it blocks literally anything from happening, when you are downloading and executing one script, not one css file or one image is downloaded. So imagine you got 10 script files loaded in your head with around 300k in size, well the browser need to load them one by one <em>(well in fact 6 at a time in FF and WebKit, and as many as 18 (or more) in IE8. In older browsers it will be one at a time. )</em>, and execute them one by one, by that time there is a good chance your page would have already render the HTML and CSS. </p>
<p>The first and very easy thing you can do is loading them just before the body end.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery/jquery.json-2.2.min.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery/plugins/formvalidator/jquery.validationEngine.js&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>That way, at the very least, the CSS and the html will be loaded before the javascript. One thing really cool about doing this is that the jQuery DOM ready statement becomes obsolete. Your html is already ready because you parsed it before any script tag. That being said, watch out for the global scope, I would still wrap my script in an anonymous self-executing function, like this:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">(function () {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;add script here &#8230;
</div>
</li>
<li class="li1">
<div class="de1">})()</div>
</li>
</ol>
</div>
<h2>Going one step further with labJS</h2>
<p>With <a target="_blank" href="http://labjs.com/">LABjs</a> you can load your js files completely asynchronous. The difference with the previous example is that even if your scripts are at the bottom, they are still downloading one by one. With this tool, you can load your scripts simultaneously and specify an executing order. From my tests with <a href="http://ajax.dynatrace.com/pages/">dynatrace</a>, the javascript load 3x more rapidly using <a href="http://labjs.com/">LABjs</a>, and my page is loading 15% faster (for about 15 js files) on IE8. An example:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">$LAB
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;/modules/comment/front/js/comment.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;/modules/core/commun/js/common.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;/modules/core/front/js/custom.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>Your loading speed gains comes with a cost of complexity. As I said earlier, it loads them async, so any of them could be executed before another. This is pretty bad if you use a library like jQuery, it needs to be executed first. You have 2 choices here, use <strong>.wait()</strong> at the end of each script that need to preserve the execution order, or use <strong>.setOptions({AlwaysPreserveOrder:true})</strong> that will always preserve the order at a small cost of performance. So if you want to go all gun blazing use .wait(), if not, use AlwaysPreserveOrder (that&#8217;s what I do, the speed cost is really minimal). Some examples:</p>
<p><strong>With .wait()</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">$LAB
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-1.4.2.min.js&quot;).wait()
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-sortable-1.7.1.custom.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery.json-2.2.min.js&quot;).wait()
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/common.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p><strong>With AlwaysPreserveOrder</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">$LAB
</div>
</li>
<li class="li1">
<div class="de1">.setOptions({AlwaysPreserveOrder:true})
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-1.4.2.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-sortable-1.7.1.custom.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery.json-2.2.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/common.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<h2>The full labJS solution</h2>
<p>Okay we figured out our execution order, what about our inline scripts? Those scripts block will probably be executed before any js file has been downloaded. Well the solution to this is cool, but not simple. We will create a global variable that will contain all our inline coding. To do that the first thing to do is add a variable to the head.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script&gt; var _loadingQueue = []; // declare our array&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/head&gt;</div>
</li>
</ol>
</div>
<p>Now in any part of your page, you could add an inline script block this way:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;_loadingQueue.push(function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;$(&quot;body&quot;).html(&quot;this is loaded from an inline script block&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;});
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>As you can see I use jquery in there, <strong>_loadingQueue.push</strong> will add this anonymous function to our loading queue, you can have as many inline scripts as you want.</p>
<p>In our labjs loading sequence, we need to do a bit of hacking to make this work.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt; var _loadingQueue = []; // declare our array&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;randomHtmlTags&#8230;&#8230;&#8230;&#8230;..&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;_loadingQueue.push(function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;$(&quot;body&quot;).html(&quot;this is loaded from an inline script block&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;});
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;randomHtmlTags&#8230;&#8230;&#8230;&#8230;..&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">var $LoadDefer = $LAB
</div>
</li>
<li class="li1">
<div class="de1">.setOptions({AlwaysPreserveOrder:true})
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-1.4.2.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery-sortable-1.7.1.custom.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/jquery.json-2.2.min.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/common.js&quot;);
</div>
</li>
<li class="li1">
<div class="de1">.wait(function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; framework.doSomething();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;});
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">if( typeof( window[ &#39;_loadingQueue&#39; ]) != &quot;undefined&quot;){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;for(var i=0,len=_loadingQueue.length; i&lt;len; i++){ &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; $LoadDefer = $LoadDefer.wait(_loadingQueue[i]);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p> As you can see I have a nice loop now. If the variable _loadingQueue exists, I get the _loadingQueue length and when all my scripts are loaded and executed ( $LoadDefer.wait). I execute all my stored functions. Pretty nice!</p>
<p>This is the full solution to load asynchronously your scripts, I think it will cover 99% of your needs. I added a demo below.</p>
<p class="clearfix"><a class="demo" href="http://www.position-relative.net/creation/optimize/" target="_blank">View demo</a></p>
<h2>Combining and minifying your scripts</h2>
<p>Something you might not notice in your local development is that file requests are time consuming. Imagine you have 10 js files in your page, if your browser spend 100ms on the network looking for each script, you lose a complete second just looking around the web for those files!</p>
<h2>Enter minify</h2>
<p><a target="_blank" href="http://code.google.com/p/minify/">Minify</a> is a nice php library that let you combine your css and js files on the fly and cache them on your server. With this library, you could get back this 1 second.</p>
<p><a target="_blank"  href="http://code.google.com/p/minify/">Minify</a> is easy to install, you download it, put it at your address root, from there you can simple access minified files like this <strong>http//www.example.com/min/?f=js/jquery/jquery-1.4.2.min.js,js/jquery/jquery-sortable-1.7.1.custom.min.js</strong>. At my workplace, we use minify on pretty much every generic plugins and components that we are using in a given site. This is a good compromise, this way we have about 10 javascript files combined and we can still see errors online from our custom scripts. But you could simply combine all the files for maximum performance, at the cost of losing errors location.</p>
<p>You might get a bit lost in the config, the 2 really important configuration are <strong>$min_cachePath</strong> and <strong>$min_documentRoot</strong>. I had to work a bit to get minify working with the Zend framework.</p>
<h2>Integrating it with labJS</h2>
<p>Once minify works, adding it is not really complicated.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script src=&#39;/js/LAB.min.js&#39;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">var $LoadDefer = $LAB
</div>
</li>
<li class="li1">
<div class="de1">.setOptions({AlwaysPreserveOrder:true})
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;/min/?f=js/jquery/jquery-1.4.2.min.js,js/jquery/jquery-sortable-1.7.1.custom.min.js&quot;).
</div>
</li>
<li class="li1">
<div class="de1">.script(&quot;js/jquery/common.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">if(_loadingQueue){ for(var i=0,len=_loadingQueue.length; i&lt;len; i++){ &nbsp;$LoadDefer = $LoadDefer.wait(_queue[i]) }}
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;</div>
</li>
</ol>
</div>
<p><strong>Small warning</strong></p>
<p>I do not use minify to combine my css files, simple because it brakes my images path. If you always use absolute path you will have no problem, but remember that most of your jQuery plugins use relative paths in their css.</p>
<h2>Conclusion</h2>
<p>Optimizing is not free, it comes with adding a layer of complexity to your application, personally I created a small helper for Zend framework that incorporate everything I mentioned above. From one boolean variable I can decided if I want to minify and embed using labJS. This is really useful for debugging. Also if you are interested in optimizing your js I would strongly recommend you have a look at the api of both <a href="http://labjs.com/" target="_blank">LABjs</a> and <a href="http://code.google.com/p/minify/" target="_blank">minify</a> for more options and more detailed instructions. </p>
<p>Some other technologies worth mentioning, <a target="_blank" href="http://developer.yahoo.com/yui/compressor/">YUI compressor</a> as a replacement for the minify library, and <a target="_blank" href="http://requirejs.org/docs/jquery.html">requireJS</a> as a replacement of LABjs. requireJS got a different approach, instead of focusing on loading asynch our files, it focus at loading only the files you need to execute a given script in the page.</p>
<p><em>Small clarification from James Burke : RequireJS also loads files asynchronously. As compared to LABjs, RequireJS encourages writing well-scoped modules, and using its built in optimization tool to combine and minify scripts.<br />
</em><br />
<strong>Dynatrace</strong><br />
<img height="383" width="500" style="border: 0pt none;" alt="dynaTrace Ajax" src="http://farm3.static.flickr.com/2610/4113481610_dae16cf451.jpg"><br />
<a href="http://ajax.dynatrace.com/pages/">Dynatrace</a> is really the tool for testing front-end performance on internet explorer, you get execution times from everything, cpu usage and much more. It is the most powerful tool I saw on any platform for front-end tracing.  The best part? It&#8217;s completely free!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/optimizing-javascriptjquery-loading-time-a-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Form Validation Engine 1.7 Online</title>
		<link>http://www.position-absolute.com/news/form-validation-engine-1-7-online/</link>
		<comments>http://www.position-absolute.com/news/form-validation-engine-1-7-online/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 15:32:37 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Random news]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2902</guid>
		<description><![CDATA[<p>It&#8217;s been a long time since I updated the validation engine. In this release the engine get a very small code overhaul, a fix to the inline ajax validation and also, and more importantly, a new feature makes its entrance:</p>
<h2>Validation&#8230;</h2>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since I updated the validation engine. In this release the engine get a very small code overhaul, a fix to the inline ajax validation and also, and more importantly, a new feature makes its entrance:</p>
<h2>Validation in overflown div and lightbox with scrollable content is now supported</h2>
<p>To get the supported mode you need to add this option when instancing your plugin:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">$(&quot;#formID&quot;).validationEngine({
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;containerOverflow:true, &nbsp;// Enable Overflown mode
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;containerOverflowDOM:&quot;#divOverflown&quot; &nbsp;// The actual DOM element container with overflow scroll on it
</div>
</li>
<li class="li1">
<div class="de1">})</div>
</li>
</ol>
</div>
<p class="clearfix"><strong>(With overflown hidden parent)</strong><br clear="all"><a class="demo" href="http://www.position-relative.net/creation/formValidator/indexOverflown.html" target="_blank">View demo</a> </p>
<p>The big change in this method is that normally the engine will append every error boxes to the body. In this case it will append every error boxes before the input validated. This add a bit of complexity, if you want the error box to behave correctly you need to wrap the input in a div being position relative, and exactly wrapping the input width and height. The easiest way to do that is by adding float:left, like in the example provided.</p>
<p>The default top right position  is currently the only supported position. Please use this mode only in  overflown div and scrollable boxes, it is slower and a bit less stable (I have been using the engine for 2 years, but only one 1 month with this method). Also, the scrolling will be applied to the overflown parent, not the document body.</p>
<h2>Thats it! Hope you like it</h2>
<p>You can download the validation engine on the <a href="http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/">API description page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/news/form-validation-engine-1-7-online/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Create an interactive Google map widget with jQuery</title>
		<link>http://www.position-absolute.com/articles/create-an-interactive-google-map-widget-with-a-list-filter-and-a-map-helper-class/</link>
		<comments>http://www.position-absolute.com/articles/create-an-interactive-google-map-widget-with-a-list-filter-and-a-map-helper-class/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 19:37:03 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2867</guid>
		<description><![CDATA[<p>Believe it or not, I rarely had to deal with the javascript Google map api, well until recently. Last week I had to create an interactive map with markers and info bubbles taken from html elements in an address list.&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Believe it or not, I rarely had to deal with the javascript Google map api, well until recently. Last week I had to create an interactive map with markers and info bubbles taken from html elements in an address list. What a better introduction to the Google map api.  These jQuery class&#8217;s are certainly not the most powerful plugins out there, but I think it can be a solid foundation for you guys that want to go beyond the scope of the article.</p>
<p>With these 2 plugins you will be able to create a map, add markers, links an information bubble to them, filter a list of addresses from CSS class elements and link them to the map.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/map/map.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/map/" target="_blank">View demo</a></p>
<h2>The Google Map jQuery Class</h2>
<p>The first thing you will want to do is, of course, create a map. For that you will have to link all files and instantiate the class.</p>
<div class="geshi no html">
<div class="head"><script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"><!--mce:0--></script></div>
<ol>
<li class="li1">
<div class="de1">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;!&#8211;mce:1&#8211;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;script src=&quot;js/helperGoogleMap.js&quot; type=&quot;text/javascript&quot;&gt;&lt;!&#8211;mce:2&#8211;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$(document).ready(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;var usamap = new HelperGoogleMap({
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;container&quot; :&quot;gMap&quot;, &nbsp; // HTML Id of the map container
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;zoom&quot; &nbsp;: 4, &nbsp; // Map Zoom Level
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;center&quot; : new google.maps.LatLng(39.639538, -97.470703), // Map starting position in latitude and Longitude
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;mapTypeId&quot; : google.maps.MapTypeId.SATELLITE // Type of map: SATELLITE, ROADMAP, HYBRID, TERRAIN
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;});
</div>
</li>
<li class="li1">
<div class="de1">});</div>
</li>
</ol>
</div>
<p>By now, you should have a map pointing on the US. Now we want to add markers and bubbles linked to them of course!</p>
<div class="geshi no html">
<div class="head">var addresses = { &#8220;markers&#8221; : [</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; {&quot;title&quot;:&quot;This is bubble text1&quot;, &quot;address&quot; : &quot;1 Learjet Way, Wichita, KS, United States&quot;},
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; {&quot;title&quot;:&quot;This is bubble text2&quot;, &quot;address&quot; : &quot;1 Croton Point Avenue, Croton On Hudson, NY, United States&quot;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;]}
</div>
</li>
<li class="li1">
<div class="de1">usamap.AddMarkers(addresses)</div>
</li>
</ol>
</div>
<p>The title will be push into the information bubble, you could also put HTML tags in there. As for the address, there is a little bit of magic happening. You can&#8217;t send directly an address into the map and get a marker, you need to use what Google call, the geocoder, to get the longitude and latitude, and after that push them to the map. You could alternatively directly use the latitude and longitude following this pattern:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
</div>
</li>
<li class="li1">
<div class="de1">var myLatlng2 = new google.maps.LatLng(-25.363882,131.044922);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">var addresses = { &quot;markers&quot; : [
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; {&quot;title&quot;:&quot;This is title 1&quot;, &quot;address&quot; : myLatlng },
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; {&quot;title&quot;:&quot;This is title 2&quot;, &quot;address&quot; : myLatlng2}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;]}
</div>
</li>
<li class="li1">
<div class="de1">usamap.AddMarkers(addresses)</div>
</li>
</ol>
</div>
<p>That&#8217;s it, each time you will call AddMarkers, the class remove all markers and you get a clean map to work with.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/map/map.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/map/" target="_blank">View demo</a></p>
<h2>The HTML List Filter Class</h2>
<p>The class will filter a list of elements based on the CSS class&#8217;s each have and compare them to currently selected select options.<br />
Let&#8217;s instantiate the class:</p>
<div class="geshi no html">
<div class="head"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script></div>
<ol>
<li class="li1">
<div class="de1">&lt;script src=&quot;js/helperListFilter.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$(document).ready(function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;var stateFilter = new HelperListFilter({
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;container&quot; :&quot;.sitesListing&quot;, &nbsp;// Filter everything in this container
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;sorting&quot; :&quot;.sort&quot;, &nbsp;// filter every elements with this class
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &quot;filter&quot; &nbsp;:[&#39;#filter&#39;,&#39;#state&#39;] &nbsp;// HTML id of the select that will be filtering (you can have as much filter as you want)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;})</div>
</li>
</ol>
</div>
<p>The class also exposes a custom event that fires when the list is filtered, that you can use this way :</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">$(document).bind(&quot;listFiltered&quot;, function() { &nbsp; &#8230; &nbsp; })</div>
</li>
</ol>
</div>
<p>If you want an one of your options to select everything, just put the value to &#8220;all&#8221;. In your list you want to add associate class on li tags that way: </p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;li class=&quot;sort california water&quot;&gt;&lt;strong&gt;Los Angeles&lt;/strong&gt;&lt;/li&gt;</div>
</li>
</ol>
</div>
<p>In this case, the class &#8220;sort&#8221; the the class to sort the li, and it will compare selected options against california and water.</p>
<p>That&#8217;s it! It will now auto filter on the onchange event.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/map/map.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/map/" target="_blank">View demo</a></p>
<h2>At last</h2>
<p>Of course you would need to link the 2 plugins together, this is done here, in the index.html:</p>
<div class="geshi no html">
<div class="head">usamap.getAddress = function(){</div>
<ol>
<li class="li1">
<div class="de1">&nbsp; var saveAddr = [];
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; $(&quot;.getMap&quot;).each(function(){ &nbsp; &nbsp;// get every sorted items
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;if($(this).css(&quot;display&quot;) != &quot;none&quot;){ // Are they visible?
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var title = $(this).find(&quot;a&quot;).html(); &nbsp;// create json to add markers
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var addr = $(this).find(&quot;.listaddress&quot;).html();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var json = {&quot;title&quot;:title, &quot;address&quot; : addr}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; saveAddr.push(json)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; })
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; var addresses = { &quot;markers&quot; :saveAddr}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; usamap.AddMarkers(addresses) &nbsp; // Add markers
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;usamap.getAddress();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;$(document).bind(&quot;listFiltered&quot;, function() { usamap.getAddress() &nbsp;}) // exposed custom event in the filtering class</div>
</li>
</ol>
</div>
<p>Here you go, hope you enjoy it!</p>
<p><strong>Tested on:</strong><br />
Firefox 3+<br />
Internet Exploder 6+<br />
Safari 4+</p>
<p>If you like and use this script, please consider buying me a beer, it&#8217;s cheap and a simple way to give back!</p>
<form style="text-align: left;" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" type="hidden" value="_s-xclick" />
<input name="encrypted" type="hidden" value="-----BEGIN PKCS7-----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-----END PKCS7-----" />
<input style="align: left;" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://www.position-absolute.com/wp-content/themes/default/images/bg_buy.jpg" type="image" />
<img src="https://www.paypal.com/en_US/i/scr/pixel.gif" border="0" alt="" width="1" height="1" /><br />
</form>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/create-an-interactive-google-map-widget-with-a-list-filter-and-a-map-helper-class/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>$.extend and $.data, more than just internal jQuery methods</title>
		<link>http://www.position-absolute.com/articles/extend-and-data-more-than-just-internal-jquery-methods/</link>
		<comments>http://www.position-absolute.com/articles/extend-and-data-more-than-just-internal-jquery-methods/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 17:04:58 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2826</guid>
		<description><![CDATA[<p>Even if I have been doing jQuery for quite sometime, I always find new cool stuff that I never used before. There is a lot of secret gems in jQuery and I wanted to go a bit deeper with 2&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Even if I have been doing jQuery for quite sometime, I always find new cool stuff that I never used before. There is a lot of secret gems in jQuery and I wanted to go a bit deeper with 2 of them, $.data and $.extend. I have been rapidly incorporating these 2 in my development, let see how these could be useful to you too.</p>
<h2>$.data, link data to DOM elements</h2>
<p>I use $.data heavily in my web apps, see $.data as a persistent memory for the DOM (until you reload the page of course, not like localstorage).  From the jQuery API:<br />
<em><br />
The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. We can set several distinct values for a single element and retrieve them later</em></p>
<p>* Just to be clear, the data is not actually saved in the DOM per say, but in the jQuery internal $.cache, and this cache will match the DOM element you saved &#8220;in it&#8221;.</p>
<h3>But how this could be useful? Let&#8217;s take a small example</h3>
<p>You have these
<divs> you want to get some properties of it, lets say width, height and its html content. Now you want to loop to get these properties for all those divs. Instead of manipulating the DOM 30 times x3 (for width, height, and html) each time you want to save those properties, you could add a json object glued to the DOM element on page load, and modify it when the DOM element is modified.</p>
<p>This way your loop will be MUCH faster. You only reach 1 json element instead of actually getting all the data one by one.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">// saving with $.data
</div>
</li>
<li class="li1">
<div class="de1">var width = $(DOMelement).width();
</div>
</li>
<li class="li1">
<div class="de1">var height= $(DOMelement).height();
</div>
</li>
<li class="li1">
<div class="de1">var html = $(DOMelement).html();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">var saveObject = {&quot;width&quot;:width, &quot;height&quot;:height, &quot;html&quot;: html };
</div>
</li>
<li class="li1">
<div class="de1">$.data(DOMelement, &quot;currentContent&quot;, saveObject );
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">/* Alternatively you can use data.fn which follow normal the setter/getter pattern */
</div>
</li>
<li class="li1">
<div class="de1">$(DOMelement).data(&quot;currentContent&quot;, saveObject );
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">// Accessing the data
</div>
</li>
<li class="li1">
<div class="de1">var currentContent = $data(DOMelement, &quot;currentContent&quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">/* Alternatively you can use data.fn which follow normal the setter/getter pattern */
</div>
</li>
<li class="li1">
<div class="de1">var currentContent = &nbsp;$(DOMelement).data(&quot;currentContent&quot;);</div>
</li>
</ol>
</div>
<p>If you need data stored in the page, this is a nice alternative than just doing variables, and it can be quite powerful. For more information visit the <a target="_blank" href="http://api.jquery.com/data/">jQuery API</a>.</p>
<h2>$.extend, extend any object</h2>
<p>If you are not developing jQuery plugins, chances are you never heard of this little method. If you have developed plugins you probably have used it to extend your plugins options. But $.extend can be used for much more than that.</p>
<p>The power of $.extend comes from the fact that you can extend any object, you could in fact merge 2 singletons into one. But where could this be useful?</p>
<p>Let&#8217;s say you have 2 complex lists that need to merge into one, you could do <strong>$.extend(true, objectReceiver, objectMerged)</strong>.<br />
objectReceiver would effectively receive everything from objectMerged.</p>
<h3>An Example</h3>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var object1 = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; apple: 0,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; banana: {weight: 52, price: 100},
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; cherry: 97
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li1">
<div class="de1">var object2 = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; banana: {price: 200},
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; durian: 100
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">$.extend(true, object1, object2);</div>
</li>
</ol>
</div>
<h3>Result:</h3>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">object1 === {apple: 0, banana: {weight: 52, price: 200}, cherry: 97, durian: 100}</div>
</li>
</ol>
</div>
<p>This technique is also really useful for configuration objects that need to be, well, configurable without always passing the complete object back. For more information visit the <a target="_blank" href="http://api.jquery.com/jQuery.extend/">jQuery API</a>.</p>
<h2>Bonus!</h2>
<p>Hope you found this article useful, for even more useful jQuery tricks I would suggest you have a look at the video below from Paul Irish, <a href="http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/">10 Things I Learned from the jQuery Source</a></p>
<p><object width="700" height="525"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12529436&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=12529436&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="700" height="525"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/extend-and-data-more-than-just-internal-jquery-methods/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Crockford on JavaScript</title>
		<link>http://www.position-absolute.com/news/crockford-on-javascript/</link>
		<comments>http://www.position-absolute.com/news/crockford-on-javascript/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 12:56:22 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Random news]]></category>
		<category><![CDATA[Videos]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2816</guid>
		<description><![CDATA[<p>Good morning everyone, having a slow day at work? take the time to listen to these videos, or just put them in background while you work!<br />
<em><br />
&#8220;During the past three months, Yahoo!&#8217;s JavaScript architect Douglas Crockford delivered a series of public&#8230;</em></p>]]></description>
			<content:encoded><![CDATA[<p>Good morning everyone, having a slow day at work? take the time to listen to these videos, or just put them in background while you work!<br />
<em><br />
&#8220;During the past three months, Yahoo!&#8217;s JavaScript architect Douglas Crockford delivered a series of public lectures on the JavaScript programming language at Yahoo!. YUI Theater  recorded and transcribed each of the five lectures.&#8221;</em></p>
<h3>Volume 1: The Early Years</h3>
<div><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=17957562&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=17957562&#038;"></embed></object></div>
<p> <br clear="all" /> </p>
<h3>Chapter 2: And Then There Was JavaScript</h3>
<div><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=18051724&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=18051724&#038;"></embed></object></div>
<p> <br clear="all" /> </p>
<h3>Act III: Function the Ultimate</h3>
<div><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=18292373&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=18292373&#038;"></embed></object></div>
<p> <br clear="all" /> </p>
<h3>Episode IV: The Metamorphosis of Ajax</h3>
<div><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=18534728&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=18534728&#038;"></embed></object></div>
<p> <br clear="all" /> </p>
<h3>Part 5: The End of All Things</h3>
<div><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=18992663&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=18992663&#038;"></embed></object></div>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/news/crockford-on-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cool web projects that will energize you</title>
		<link>http://www.position-absolute.com/news/cool-web-projects-that-will-energize-you/</link>
		<comments>http://www.position-absolute.com/news/cool-web-projects-that-will-energize-you/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 15:37:03 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Random news]]></category>
		<category><![CDATA[Videos]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2788</guid>
		<description><![CDATA[<p>Sometimes there are projects that are just way cooler than yours. In this spirit, I wanted to highlights some projects that just flabbergast me. I tried to put more down to earth projects than just &#8220;experimental stuff&#8221;.</p>
<h2>Vanthia</h2>
<p><a target="_blank" href="http://www.vanthia.com/">Vanthia</a> is <em>just</em> a browser MMO&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Sometimes there are projects that are just way cooler than yours. In this spirit, I wanted to highlights some projects that just flabbergast me. I tried to put more down to earth projects than just &#8220;experimental stuff&#8221;.</p>
<h2>Vanthia</h2>
<p><a target="_blank" href="http://www.vanthia.com/">Vanthia</a> is <em>just</em> a browser MMO based on Mootools and Python. The creator worked  on it for years and it shows. The UI is great, the game itself looks great, it use canvas and various HTML5 apis. If you are trying to do an MMO javascript based games, I suggest you have a good look at it.</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/uMn4Sijglz8&#038;hl=en_GB&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/uMn4Sijglz8&#038;hl=en_GB&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><br />
The video is a bit old, it changed quite a bit. </p>
<h2>Aves game engine</h2>
<p><a target="_blank" href="http://www.dextrose.com/en/projects/aves-engine">Aves</a> is a javascript game engine that got an isometric level editor. oh yes, it also got real time features.<br />
<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/Ol3qQ4CEUTo&#038;hl=en_GB&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Ol3qQ4CEUTo&#038;hl=en_GB&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<h2>Mozilla Hacks HTML5 image editor</h2>
<p>This one is more of a <a target="_blank" href="http://hacks.mozilla.org/2010/02/an-html5-offline-image-editor-and-uploader-application/">technical demo</a>, but anyway, can you ask more than drag and dropping files directly to your browser, saving them offline and synchronize them later? It really show what kind of user interaction we will be able to do with HTML5.<br />
<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/wbSoSCStodA&#038;hl=en_GB&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wbSoSCStodA&#038;hl=en_GB&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
<h2>Jilion HTML5 video player</h2>
<p>This HTML5 video player has nothing to envy to any flash video player.<br />
<a target="_blank" href="http://jilion.com/sublime/video">http://jilion.com/sublime/video</a></p>
<h2>Quake 2 engine to HTML5</h2>
<p>When 3 googlers decide to <a target="_blank" href="http://code.google.com/p/quake2-gwt-port/">port Quake 2</a> to your browser you can get surprising results. Oh and did I told you it got multiplayer using HTML5 web sockets?  it also use localstorage, and html5 audio.<br />
<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/fyfu4OwjUEI&#038;hl=en_GB&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/fyfu4OwjUEI&#038;hl=en_GB&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/news/cool-web-projects-that-will-energize-you/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using the jQuery AOP plugin to debug your javascript</title>
		<link>http://www.position-absolute.com/articles/using-the-jquery-aop-plugin-to-better-debug-your-javascript/</link>
		<comments>http://www.position-absolute.com/articles/using-the-jquery-aop-plugin-to-better-debug-your-javascript/#comments</comments>
		<pubDate>Wed, 26 May 2010 00:50:41 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2729</guid>
		<description><![CDATA[<p>Debugging javascript code is not always easy, you have your normal programming flow, but you also have a bunch of event listeners attached to DOM elements that do not really follow a specific pattern.</p>
<p>Hunting listeners in JS files can be&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Debugging javascript code is not always easy, you have your normal programming flow, but you also have a bunch of event listeners attached to DOM elements that do not really follow a specific pattern.</p>
<p>Hunting listeners in JS files can be quite tedious, this is where the <a target="_blank" href="http://code.google.com/p/jquery-aop/">AOP plugin</a> can help you.</p>
<h2>What is Aspect Oriented Programming? (AOP)</h2>
<p><em>&#8220;In computing, aspect-oriented programming (AOP) is a programming paradigm which isolates secondary or supporting functions from the main program&#8217;s business logic. It aims to increase modularity by allowing the separation of cross-cutting concerns, forming a basis for aspect-oriented software development.&#8221;</em></p>
<p><a target="_blank" href="http://www.ibm.com/developerworks/rational/library/2782.html">A good AOP article</a></p>
<p>In javascript, using this technique, you can bind listeners to any function or object, which will be fired when the function is executed. With this information, you can know when a function is executed and what DOM element fired it.</p>
<p>The <a target="_blank" href="http://code.google.com/p/jquery-aop/">jQuery AOP plugin</a> is very small and it allows to add advices (Before, After, After Throw, After Finally, Around and Introduction) to any global or instance object. </p>
<p class="clearfix"> <a class="demo" href="http://www.position-relative.net/creation/debug/" target="_blank">View demo</a></p>
<p><strong>* Have your firebug or developer console open</strong></p>
<h2>How to use it</h2>
<p>Using the plugin is pretty simple, first add the script to your head (you can <a target="_blank" href="http://code.google.com/p/jquery-aop/">download it here</a>)</p>
<p>An example:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">jQuery.aop.before( {target: String, method: &#39;replace&#39;},
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function(regex, newString) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console.log(&quot;About to replace string &#39;&quot; + this + &quot;&#39; with &#39;&quot; + newString + &quot;&#39; using regEx &#39;&quot; + regex + &quot;&#39;&quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">);</div>
</li>
</ol>
</div>
<p>When a string is replaced you will have this log fired, pretty sweet.</p>
<h2>Lets add a little twist to it</h2>
<p>What if we want to test any function easily?</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var debug = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;customConnect : function(instance){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jQuery.aop.before( {target: eval(instance.object), method: instance.method}, function(object) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&#39;Function Fired : &#39; + instance.object + &quot;.&quot; + instance.method );
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&quot;DOM caller : &quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(object);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&quot;object state : &quot;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(this);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&#39;&#8212;&#8212;&#8212;-&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; );
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">}</div>
</li>
</ol>
</div>
<p>As you can see, you can now bind basically everything using this method. Want to know when your lightbox is fired? Or maybe when this particularly function is called? Pretty easy.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp; debug.customConnect({&quot;object&quot;: &quot;appModal&quot;, &quot;method&quot;: &quot;load&quot;})
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; debug.customConnect({&quot;object&quot;: &quot;window&quot;, &quot;method&quot;: &quot;test&quot;}) &nbsp;// to test an inline function</div>
</li>
</ol>
</div>
<p>That would be how you use it. In this example I fire logs before loading my lightbox and before using the test() function.</p>
<h2>That&#8217;s it</h2>
<p>This is just a start, you could also log any DOM modification, this plugin can really give you a lot of debugging power.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/debug/debug.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/debug/" target="_blank">View demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/using-the-jquery-aop-plugin-to-better-debug-your-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A javascript module framework starter</title>
		<link>http://www.position-absolute.com/articles/a-javascript-module-starter/</link>
		<comments>http://www.position-absolute.com/articles/a-javascript-module-starter/#comments</comments>
		<pubDate>Fri, 21 May 2010 01:20:24 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>
		<category><![CDATA[Small Projects & plugins]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2466</guid>
		<description><![CDATA[<p><em>This is the following article to <a href="http://www.position-absolute.com/articles/javascript-patterns-headache-the-path-to-modular-applications-part-1/">part 1 on javascript patterns</a>.</em></p>
<h2>A namespace to rule them all</h2>
<p>This is a small javascript framework that I use for starting my javascript/jQuery project. I think it could be most beneficial for those coming from&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><em>This is the following article to <a href="http://www.position-absolute.com/articles/javascript-patterns-headache-the-path-to-modular-applications-part-1/">part 1 on javascript patterns</a>.</em></p>
<h2>A namespace to rule them all</h2>
<p>This is a small javascript framework that I use for starting my javascript/jQuery project. I think it could be most beneficial for those coming from libraries like jQuery. Dojo being quite expressive on how to work with it, and Mootools and Prototype being a bit more structured.</p>
<p><em>A small note, I used jQuery in the framework because of the AOP plugin (and because I mainly work with jQuery), you can erase any jQuery reference easily in less than 30 seconds .</em></p>
<p><strong>What this framework aims to do:</strong></p>
<ul>
<li>Easy to understand</li>
<li>Modular</li>
<li>Each module will heritage a set of tools and each module can be instantiate</li>
<li>One namespace</li>
<li>Can apply tests or unit test out side of the application itself.</li>
<li>Use the jQuery aop plugin (aspect oriented programming) to add logs to internal functions</li>
</ul>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/frameworkJS/frameworkJS.zip">Download the source code</a></p>
<p><strong>** I am not an expert in javascript design patterns, but that is what I have come to in light of my experience, I will certainly take constructive comments, and if you like it, please use and modify it as you want.</strong></p>
<h2>Schema</h2>
<p><img border="0" src="/wp-content/themes/default/images/PSIMG/uml.jpg"><br />
<a target="_blank" href="/wp-content/themes/default/images/PSIMG/DIAGRAM-Architecture-JS-v0.1.pdf">Download the full version in pdf</a></p>
<h2>How it works</h2>
<p><strong>LABjs to load all your scripts</strong><br />
First thing first, in app.html you will see that JS files are loaded asynchronously from the bottom body tag with <a href="http://labjs.com/">LABjs</a>. This tools will make you js files load faster and your script will no longer block page resources download (images, etc). You can use .wait() to define load order if necessary. Have a look at the <a href="http://labjs.com/documentation.php">documentation</a>. The major drawback from loading asynchronously is that you might have a situation where someone clicked on a button and the script associate with it might not be loaded.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$LAB
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;.script(&quot;framework.js&quot;).wait()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;.script(&quot;plugin.framework.js&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;.script(&quot;myplugin.framework.js&quot;).wait()
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;.script(&quot;init.js&quot;);
</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;</div>
</li>
</ol>
</div>
<p><strong>Namespace</strong><br />
I try to use only one or 2 namespaces for all my applications or even websites. This is why in objectUtils.construct.js you will see that the global namespace is define by this line:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">/* Global App Namespace */
</div>
</li>
<li class="li1">
<div class="de1">var ApplicationNamespace = {}</div>
</li>
</ol>
</div>
<p>Each module will be declared using this object. So for example, a lightbox class would be declared this way :  ApplicationNamespace.lightboxClass = function(){}</p>
<h2>Modules</h2>
<p>A typical module in your app should look likes this:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">ApplicationNamespace.Classname = function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; // constructor &nbsp; &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; function privateFunctionInit() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // private
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; this.privilegedFunction = function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // privileged
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; privateFunction();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;privateFunctionInit()
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li1">
<div class="de1">ApplicationNamespace.Classname.prototype = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; utils: object.create(namespace.moduleUtils),
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Method: function(){}
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">var class = new ApplicationNamespace.Classname();</div>
</li>
</ol>
</div>
<p>We have a pretty cool module here. Let&#8217;s get a bit into details here.</p>
<p>This is a private function that will not be available in the global scope. These method cannot also be tested externally, unless doing a small trick that I will talk later on.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">function privateFunctionInit() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // private
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }</div>
</li>
</ol>
</div>
<p>This is our privileged function that can access private functions and can be accessed outside after we instantiate it by using, in this case :   class.privilegedFunction</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp;this.privilegedFunction = function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // privileged
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; privateFunction();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; };</div>
</li>
</ol>
</div>
<p>This will be called after we instanciate (var class = new namespace.Classname()) and automatically launch this init function. You should also note that this might happen before the DOM is ready, if you use jQuery you need the DOM ready wrapper here.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">privateFunctionInit()</div>
</li>
</ol>
</div>
<p>This is where we declare the prototype associate at our module and method shared by every instance of the module.You can have more informations on prototype in my other article.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">ApplicationNamespace.Classname.prototype = {</div>
</li>
</ol>
</div>
<p>This is where we get or create the utility module shared by every modules.</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; utils: object.create(namespace.moduleUtils),</div>
</li>
</ol>
</div>
<p>And finally we instantiate the module. There is probably a lot of modules that you might have to instantiate only one time. </p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var class = new ApplicationNamespace.Classname();</div>
</li>
</ol>
</div>
<h2>Modules utilities object</h2>
<p>The utility object is shared across every modules. the definition is done in objectUtils.construct.js.</p>
<p>This is notably important if you want to run some unit test or run some debug from outside your modules. One thing I really like to do,is using the jQuery AOP (Aspect oriented) plugin. This will allow you to bind debug methods to any functions in any modules, it also give you the DOM caller if it exist and it&#8217;s context. Here is an example:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">// WRITE TEST METHODS
</div>
</li>
<li class="li1">
<div class="de1">moduleUtils.debug = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;live : true,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;loadModal : function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; jQuery.aop.before( {target: appModal, method: &#39;load&#39;}, function(object) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&#39;Modal opened&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(object);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(this);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; console.log(&#39;&#8212;&#8212;&#8212;-&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; );
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;},
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">$(document).ready(function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;// USE TEST METHODS
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;if(moduleUtils.debug.live == true){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; appModal.utils.debug.loadModal();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">});</div>
</li>
</ol>
</div>
<p>As you can see the object is created even if live is false (in production you might want to enable and disable it often), normally this file should not be loaded when the project goes live.</p>
<h2>Unit Tests</h2>
<p>I really wanted to include QUnit as a part of my work flow, unfortunately, you can&#8217;t load QUnit dynamically for now. I really want my unit tests to be in a small lightbox and decide to load it in my app from a small variable state. I will probably create a really small unit test plugin in the near future, I think this is really important to test regressions.  </p>
<h2> That&#8217;s it, here it is</h2>
<p>I am certainly not going to say this is a perfect solution, but that&#8217;s what I use for now, it will certainly evolve with time.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/frameworkJS/frameworkJS.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/frameworkJS/app.htm" target="_blank">View demo</a></p>
<p><strong>Version 0.5 Online</strong></p>
<p><br clear="all" /><br />
If you like and use this script, please consider buying me a beer, it&#8217;s cheap and a simple way to give back!</p>
<form style="text-align:left;" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input name="cmd" type="hidden" value="_s-xclick" />
<input name="encrypted" type="hidden" value="-----BEGIN PKCS7-----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-----END PKCS7-----" />
<input style="align:left" alt="PayPal - The safer, easier way to pay online!" name="submit" src="http://www.position-absolute.com/wp-content/themes/default/images/bg_buy.jpg" type="image" />
<img src="https://www.paypal.com/en_US/i/scr/pixel.gif" border="0" alt="" width="1" height="1" /><br />
</form>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/a-javascript-module-starter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript patterns headache, the path to modular applications (part 1)</title>
		<link>http://www.position-absolute.com/articles/javascript-patterns-headache-the-path-to-modular-applications-part-1/</link>
		<comments>http://www.position-absolute.com/articles/javascript-patterns-headache-the-path-to-modular-applications-part-1/#comments</comments>
		<pubDate>Wed, 19 May 2010 20:23:17 +0000</pubDate>
		<dc:creator>Cedric Dugas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Javascript / jQuery]]></category>

		<guid isPermaLink="false">http://www.position-absolute.com/?p=2449</guid>
		<description><![CDATA[<p>With libraries like jQuery, manipulating the DOM, ajax calls, animating has become really trivial. The problem is, it also becomes easy to have a mess of DOM ready statements without any comments or structure.</p>
<p>One thing most javascript developers start to&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>With libraries like jQuery, manipulating the DOM, ajax calls, animating has become really trivial. The problem is, it also becomes easy to have a mess of DOM ready statements without any comments or structure.</p>
<p>One thing most javascript developers start to do at some point is use objects literal to structure their code. This is a start, but currently creating a front-end intensive web app, I started to wonder if I could do better.</p>
<p>My goal was:</p>
<ul>
<li>Easy to understand</li>
<li>Modular</li>
<li>Each module will inherit a set of tools and can be instantiated</li>
<li>One namespace</li>
<li>Can apply tests or unit test from outside of the application.</li>
<li>Can use the aop plugin (aspect oriented programming) to add logs to internal functions</li>
</ul>
<p>First, I would suggest you look at a couple of resources if you are not too familiar with javascript patterns.</p>
<ul>
<li>Alex Saxton on <a href="http://alexsexton.com/?p=51">javascript and jQuery pattern</a></li>
<li><a href="http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code/">Using Objects to Organize Your Code</a> &#8211;  Rebecca Murphey</li>
<li><a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742">Javascript: The Good Parts</a> (Chapter 5) (book) -Douglas Crockford</li>
<li><a href="http://javascript.crockford.com/prototypal.html">Prototypal Inheritance in Javascript</a> -Douglas Crockford</li>
<li><a href="http://www.manning.com/resig/">The secrets of the Javascript Ninja</a> -John Resig</li>
<li><a href="http://apress.com/book/view/159059908x">Pro Javascript design patterns</a> &#8211; By Ross Harmes, Dustin Diaz</li>
</ul>
<p>Some nice stackoverflow discussions:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/366647/are-design-patterns-in-javascript-helpful-and-what-exactly-are-they">Are design patterns in JavaScript helpful? And what exactly are they?</a></li>
<li><a href="http://stackoverflow.com/questions/24642/what-are-some-examples-of-design-pattern-implementations-using-javascript">What Are Some Examples of Design Pattern Implementations Using JavaScript?</a></li>
<li><a href="http://stackoverflow.com/questions/7002/what-javascript-patterns-do-you-use-most">What JavaScript patterns do you use most?</a></li>
</ul>
<p><br clear="all" /></p>
<p> With this in mind, 3 patterns sticked more than the others, let&#8217;s check them all.</p>
<p><strong><br />
** I am not an expert at javascript patterns and inheritance but I think it was nice to share my findings, please feel free to discuss and correct this article in the comments if necessary.</strong></p>
<h2>Object Literals</h2>
<p>Object literals are great. You get a pattern that is simple and easy to organize, everything is public, everything can be overwritten, so you don&#8217;t have to worry too much about having things you can&#8217;t access. Easy to follow and understand, even for new comers.</p>
<p><strong>A typical object literal:</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var module= {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;init : function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; module.load();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;},
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;load : function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; /* instructions */
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">/* Load Init in your script */
</div>
</li>
<li class="li1">
<div class="de1">module.init()</div>
</li>
</ol>
</div>
<h2>The &#8220;self executing&#8221; revealing module pattern</h2>
<p>The <a href="http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code/">module pattern</a> is a little bit more complex than a simple object literal, that being said, it gives you more power over what you want to be private and public. Basically you hide all your module behind a closure, this closure makes everything in the function private. You then use return {} to make a reference to a function you want public.</p>
<p>Now I will not get in the private or not debate, but I will say that <a href="http://ajaxian.com/archives/a-deep-dive-and-analysis-of-the-javascript-module-pattern">some</a>  <a href="http://snook.ca/archives/javascript/no-love-for-module-pattern">people</a> disagree with this pattern. </p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var revealingModulePattern = function(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; var privateVar = 1;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function privateFunction(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; alert(&#39;private&#39;);
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; var publicVar = 2;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function publicFunction(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; anotherPublicFunction(); &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; function anotherPublicFunction(){
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; privateFunction();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; // reveal all things private by assigning public pointers
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; return {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; init:publicFunction,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; count:publicVar,
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; increase:anotherPublicFunction
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">}();
</div>
</li>
<li class="li1">
<div class="de1">revealingModulePattern.init();</div>
</li>
</ol>
</div>
<p>As you can see, you create a public reference to the private function. That was my main problem with it. You never really have access to the public function, you just use a bridge to fire it. It creates problems when you want to bind something to a public function, it will never fire if this function is fired internally. Also, You can&#8217;t modify this object.</p>
<h2>The module pattern, prototype style</h2>
<p>Ahh prototype, those damn prototypes. I found this pattern gave me more liberty, and at the same time, a way to really use my public functions and not just reference them. On the question, but what is a prototype? Let&#8217;s just put it this way without going into details: </p>
<p>In the example below, <a href="http://">namespace.Classname.prototype</a> will be referenced by every instance you create with &#8220;new namespace.Classname();&#8221;. So a prototype would normally exist 1 time, if you would create 2 instances, and add a new prototype method, both instances would see it. In brief, when you create a new instance, everything in &#8220;namespace.Classname = function() {&#8221; is cloned, everything in &#8220;namespace.Classname.prototype&#8221; is not cloned, and is shared with all instances.</p>
<p>Here what it looks like:</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var namespace = {};
</div>
</li>
<li class="li1">
<div class="de1">namespace.Classname = function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; // constructor &nbsp; &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; function privateFunctionInit() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // private
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; this.privilegedFunction = function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; // privileged
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; privateFunction();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; };
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;privateFunctionInit()
</div>
</li>
<li class="li1">
<div class="de1">};
</div>
</li>
<li class="li1">
<div class="de1">namespace.Classname.prototype.aMethod =function() {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; this.privilegedFunction();
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">namespace.Classname.prototype.anotherMethod = function() {}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">var class = new namespace.Classname();</div>
</li>
</ol>
</div>
<p>Now this is really not what <a href="http://higher-order.blogspot.com/2008/02/javascript-parasitic-inheritance-super.html">Crockford would advocate</a>, but still, I found this was the pattern that was suiting best my &#8216;global&#8217; needs.</p>
<p>Hope this helps you in your quest of modular javascript <img src='http://www.position-absolute.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . <a href="http://www.position-absolute.com/articles/a-javascript-module-starter/">In part 2</a>, I explain my javascript &#8220;framework&#8221;, this is really just my starting point for creating apps, or even just a website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.position-absolute.com/articles/javascript-patterns-headache-the-path-to-modular-applications-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
