<?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>Integralist</title>
	<atom:link href="http://www.integralist.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.integralist.co.uk</link>
	<description>Web Development and Integral Theory</description>
	<lastBuildDate>Sun, 04 Jul 2010 21:11:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>JavaScript and Web Performance books you need to read</title>
		<link>http://www.integralist.co.uk/javascript/javascript-and-web-performance-books-you-need-to-read/</link>
		<comments>http://www.integralist.co.uk/javascript/javascript-and-web-performance-books-you-need-to-read/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 21:41:37 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[OOP]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=434</guid>
		<description><![CDATA[This is a list of some JavaScript based web development books I own and which I can fully recommend to any aspiring JavaScript ninjas. I describe below what I thought of each book and why I like them enough to recommend them&#8230; Pro JavaScript Techniques Pro JavaScript Design Patterns Pro JavaScript RIA Techniques Object Oriented [...]]]></description>
			<content:encoded><![CDATA[<p>This is a list of some JavaScript based web development books I own and which I can fully recommend to any aspiring JavaScript ninjas. I describe below what I thought of each book and why I like them enough to recommend them&#8230;</p>
<ul>
<li><a href="#a">Pro JavaScript Techniques</a></li>
<li><a href="#b">Pro JavaScript Design Patterns</a></li>
<li><a href="#c">Pro JavaScript RIA Techniques</a></li>
<li><a href="#d">Object Oriented JavaScript</a></li>
<li><a href="#e">JavaScript: The Good Parts</a></li>
<li><a href="#f">High Performance Websites</a></li>
<li><a href="#g">Even Faster Websites</a></li>
<li><a href="#h">High Performance JavaScript</a></li>
</ul>
<p><span id="more-434"></span></p>
<h2 id="a">Pro JavaScript Techniques</h2>
<p>This is one of the first <b>proper</b> books on the subject of JavaScript I ever read (<em>the very first book on JavaScript I read I actually still own! I keep it as a reminder of where I began and I really chuckle as I flick through its pages and see the seriously shite advice it recommends as &#8216;best practices&#8217;. The book was &#8220;Sams Teach Yourself JavaScript in 24hrs&#8221; published back in 2001 &#8211; a horrid book that I do NOT recommend!</em>).</p>
<p>But back to <strong>Pro JavaScript Techniques</strong>&#8230; I found a lot of errors in the book (in a few places there was code that just simply didn&#8217;t work and when I went online and to forums looking for answers &#8211; as the errata for the book wasn&#8217;t any help &#8211; I found a whole lot of people hating on the author John Resig <a href="http://ejohn.org/">http://ejohn.org/</a> saying it wasn&#8217;t surprising for me to find code he had written didn&#8217;t work). </p>
<p>Now this probably doesn&#8217;t seem like much of a recommendation, but in fact I still look back at <strong>Pro JavaScript Techniques</strong> and I can see it really opened my eyes to <strong>a lot</strong> of cross browser inconsistencies that I never would have ever dreamed possible, and although there were a few places where the scripts appeared to be broken, there were still lots of good code examples that successfully worked around the painful cross browser inconsistencies/quirks discussed in the book. </p>
<p><strong>Pro JavaScript Techniques</strong> was published back in 2006 (around the time of jQuery&#8217;s initial release) so I know there was a lot that the author had learned about the language since that time through working on jQuery and so this book could probably do with an updated edition, but I still recommend it in its current form none the less.</p>
<h2 id="b">Pro JavaScript Design Patterns</h2>
<p>This is a very intense reference on JavaScript Design Patterns and might be a little too much for people new to JavaScript, but I think users of all skill levels will get a lot of mileage from it. I personally learnt a great deal about object oriented programming (OOP) from this book and for the OOP chapter alone I recommend it. </p>
<p>The design patterns are all very well explained with good working examples which you can use to help get your head around how you would use the patterns in the real world (one of my pet peeves are technical books that have weak &#8216;real world examples&#8217; that are so basic that you would never consider them &#8216;real world&#8217; &#8211; but all code examples are very detailed and really do a great job of properly representing the power of the patterns they are teaching).</p>
<p>The book then progresses into the subject of inheritance within JavaScript, both classical and prototypal, and explains the pros and cons to both varieties. It shows you how to implement Interfaces, discusses encapsulation and information hiding through private members as well as the benefits and drawbacks of these techniques and then moves on through to the design patterns themselves.</p>
<p>What I liked about the book (especially the opening chapters on <strong>Object-Oriented Programming</strong>) is that they show you a basic &#8216;bare bones&#8217; solution, the author(s) then explain the limitations of that particular solution and move on to a more advanced option. This next solution is also flawed and so they again discuss the possible limitations, and then finally they move on and explain a complete solution that incorporates all required functionality and features while extending the example to work around the previous limitations discussed. Thus leaving you with a fully working example where you now have a solid understanding of the design pattern and the code that implements it.</p>
<h2 id="c">Pro JavaScript RIA Techniques</h2>
<p>This book seemed to be remarkably up to date when I bought it, including HTML5 features, cutting edge AJAX as well as SVG/Canvas implementations along with great information on how to improve your UI&#8217;s &#8216;perceived&#8217; responsiveness.</p>
<p>It discusses everything from setting up a development environment to coding style guidelines and object-oriented JavaScript, to actually building your own JavaScript library (including things like advanced event and ajax management) and helping you understand the problems with performance critical applications and how to avoid common pitfalls. It also discusses typography and associated libraries and how to use them, and the same for multimedia playback and offline storage (all explained and included in the &#8216;build your own JavaScript library&#8217; code) and finally it covers accessiblity in ajax applications.</p>
<p>An absolutely brilliant book on current web development trends and technologies.</p>
<h2 id="d">Object Oriented JavaScript</h2>
<p>I haven&#8217;t read David Flanagan&#8217;s world renowned book &#8220;JavaScript: The Definitive Guide&#8221; so I can&#8217;t say if this book is better, although from flicking through &#8220;The Definitive Guide&#8221; I know this book wont be any where near as thorough as it isn&#8217;t supposed to be a complete reference guide but more an up to date book on how to learn JavaScript in the modern environment and take advantage of lessons learned over the past few years since JavaScript has seen such a resurgence in popularity.</p>
<p>I found this an enjoyable book to read and a good &#8216;general&#8217; reference for most issues I run into. If you&#8217;re learning JavaScript for the first time then this is the book I suggest you read.</p>
<h2 id="e">JavaScript: The Good Parts</h2>
<p>This book ideally should be read by anyone who already has a good understanding of JavaScript and uses it in their daily lives. It will not teach you JavaScript and will fundamentally be useless to anyone wanting to learn JavaScript and will only explain why the author Douglas Crockford <a href="http://javascript.crockford.com/">http://javascript.crockford.com/</a> (who is a celebrated JavaScript guru) thinks certain aspects of the language are dangerous, confusing and in some cases just plain wrong.</p>
<p>Now it would seem that over the years developers have come to not take everything Crockford says as gospel (which is definitely a healthy way to go) but I do feel this is a great book to have in your collection and although there may be one or two areas for disagreement, on the whole I think he has put in place valid arguments and a solid dissection of the JavaScript language.</p>
<h2 id="f">High Performance Websites</h2>
<p>I love this book and if you need to understand how and why your site is running slow (or better still, why it doesn&#8217;t run as fast as other websites) then this is the book to read.</p>
<p>It starts off explaining about HTTP and content-headers, compression, expires and the purpose of Keep-Alive and in general how that whole aspect of a web application works. This is important because if you don&#8217;t understand the core concepts of how the web works then how can you begin to understand where to improve your code. It&#8217;s imperative to understand these concepts to help your application performance.</p>
<p>For me, at the time when I first read this book (back in 2007) one of the most important bits of information in this book came from the chapter on how JavaScript files block parallel downloads and solutions to work around that critical issue (something which is now almost common practice among front-end engineers).</p>
<p>It also covers topics such as caching Ajax requests, how best to handle CSS and avoiding CSS expressions, reducing DNS lookups, minifying JavaScript, avoiding redirects, configuring ETags (and how to remove them as they can cause hell with proxy servers) and finishes up with the deconstruction of the top 10 sites on the internet to help you understand what aspects of performance they are missing/neglecting.</p>
<h2 id="g">Even Faster Websites</h2>
<p>This is the follow up to the previous book &#8220;High Performance Website&#8221; and actually covers more JavaScript related issues such as Ajax performance, how to create responsive web applications such as measuring latency and threading (Web Workers, Google Gears, JavaScript Timers and Virtual Memory).</p>
<p>It also discusses flushing the content of the page early and in stages so as to improve perceived performance. It goes into further detail about how to stop JavaScript from blocking downloads and loading them in parallel with other JavaScript files and coupling asynchronous scripts. It briefly goes into writing efficient JavaScript such as managing scope, efficient data access, string optimisations and how to avoid the dreaded long running script error.</p>
<p>It also covers subjects such as advanced Ajax (in the form of Comet <a href="http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications">http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications</a>), going beyond GZIP&#8217;ing, optimising images, using domain sharding to improve parallel downloading in certain browsers and simplifying CSS selectors.</p>
<p>All in all a very thorough book on performance issues.</p>
<h2 id="h">High Performance JavaScript</h2>
<p>This book covers a lot of the JavaScript topics found in &#8220;High Performance Website&#8221; and &#8220;Even Faster Websites&#8221; but includes a whole lot more on top of that for the hardcode JavaScript ninjas. And regardless, it&#8217;s actually great to have all this information under one title rather than spread across two separate books.</p>
<p>The context of the book was to take the reader through the complete web development life-cycle, from start to finish, so some of the subjects it looks into are the loading and executing and JavaScript and how to avoid blocking parallel downloads. It moves onto data access and scope management, DOM scripting, paints and reflows, event delegation, algorithms and flow control, strings and regular expressions, responsive user interfaces, ajax performance guidelines and programming best practices and then moves onto the deployment of your application and the testing and debugging with various tools.</p>
<p>To make this book even better though is that its chapters are written by a who&#8217;s who of the JavaScript elite such as&#8230; (note: the below list is taken from main author Nicholas C. Zakas website)</p>
<ul>
<li><a href="http://techfoolery.com/">Ross Harmes</a>, co-author of <a href="http://www.amazon.com/gp/product/159059908X?ie=UTF8&amp;tag=nczonline-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=159059908X">Pro JavaScript Design Patterns</a> and front-end engineer for <a href="http://www.flickr.com">Flickr</a>. Ross writes about Ajax performance with an eye on squeezing out the fastest client-server messaging possible.</li>
<li><a href="http://www.julienlecomte.net/">Julien Lecomte</a>, creator of the YUI Compressor and front-end engineer on <a href="http://search.yahoo.com">Yahoo! Search</a>. Julien describes effectively deploying your JavaScript with performance in mind.</li>
<li><a href="http://blog.stevenlevithan.com/">Steven Levithan</a>, co-author of the <a href="http://www.amazon.com/gp/product/0596520689?ie=UTF8&amp;tag=nczonline-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0596520689">Regular Expression Cookbook</a> and guru on all things related to strings and regular expressions. Steven explains the ins and outs of string and regular expression performance in JavaScript in a way that I believe has never been done before.</li>
<li>Matt Sweeney, <a href="http://developer.yahoo.com/yui/3/">YUI 3</a> architect. Matt focuses on currently available tools for measuring JavaScript performance.</li>
<li><a href="http://www.phpied.com">Stoyan Stefanov</a>, author of <a href="http://www.amazon.com/gp/product/1847194141?ie=UTF8&amp;tag=nczonline-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1847194141">Object-Oriented JavaScript</a> and front-end engineer on <a href="http://search.yahoo.com">Yahoo! Search</a>. Stoyan investigates the performance of the DOM and how reflows and repaints affect JavaScript&#8217;s perceived performance. This chapter, to me, will be the definitive print resource for reflow information.</li>
</ul>
<h3>Conclusion</h3>
<p>So there you have it, that&#8217;s my run down of the current list of web development and JavaScript related books I think you should own.</p>
<p>Feel free to share your thoughts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/javascript-and-web-performance-books-you-need-to-read/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript&#8217;s Regular Expression exec/lastIndex bug?</title>
		<link>http://www.integralist.co.uk/javascript/javascripts-regular-expression-exec-lastindex-bug/</link>
		<comments>http://www.integralist.co.uk/javascript/javascripts-regular-expression-exec-lastindex-bug/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 20:23:25 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Bug]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=435</guid>
		<description><![CDATA[There is a slight bug in how *certain* browsers handle JavaScript&#8217;s Regular Expression exec() method, and the two legends of Regular Expressions&#8230; Jan Goyvaerts (www.regexguru.com) Steven Levithan (blog.stevenlevithan.com) &#8230;(both co-authors of the Regular Expressions Cookbook) seem to be in disagreement with each other as to how Internet Explorer implements this. According to Steven, IE&#8217;s implementation [...]]]></description>
			<content:encoded><![CDATA[<p>There is a slight bug in how *certain* browsers handle JavaScript&#8217;s Regular Expression <code>exec()</code> method, and the two legends of Regular Expressions&#8230;</p>
<ol>
<li>Jan Goyvaerts (<a href="http://www.regexguru.com/">www.regexguru.com</a>)</li>
<li>Steven Levithan (<a href="http://blog.stevenlevithan.com/">blog.stevenlevithan.com</a>)</li>
</ol>
<p>&#8230;(both co-authors of the <a href="http://oreilly.com/catalog/9780596520694/">Regular Expressions Cookbook</a>) seem to be in disagreement with each other as to how Internet Explorer implements this.</p>
<p><span id="more-435"></span></p>
<p>According to Steven, IE&#8217;s implementation is a bug (<a href="http://blog.stevenlevithan.com/archives/exec-bugs">http://blog.stevenlevithan.com/archives/exec-bugs</a>). In Jan&#8217;s mind, IE actually did what was the right thing to do and changed the feature to work similarly to how the other RegEx methods work (although, as usual, IE isn&#8217;t following the standard as set out by the ECMAScript specification &#8211; <a href="http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/">http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/</a>).</p>
<p>I personally am in agreement with Jan in that I think how IE implemented the <code>lastIndex</code> property is the best way of avoiding a rare (but still possible) occurrence of an infinite loop with browsers that don&#8217;t increment the <code>lastIndex</code> value. Although! I fully understand where Steven is coming from with regards to poor code design as <code>lastIndex</code> is used for two separate purposes within ECMAScript and so by IE fixing the issue with <code>lastIndex</code> not being properly incremented when finding a zero-width match it in fact could end up causing more confusion when used along with the RegEx <code>test</code> method (again read Steven&#8217;s explanation here: <a href="http://blog.stevenlevithan.com/archives/exec-bugs#comment-17444">http://blog.stevenlevithan.com/archives/exec-bugs#comment-17444</a> for more details).</p>
<p>The <code>lastIndex</code> property has a few purposes in JavaScript such as:</p>
<ul>
<li>Specifying where the string search should begin (you would need the global modifier flag <code>/g</code> set for this bug to be an issue)</li>
<li>Tracking the position of where the next search should start</li>
<li>Indicating where the last match ended</li>
</ul>
<p>But if for example your regex is able to succeed with a zero-width match (such as <code>^</code>) and the browser doesn&#8217;t increment the <code>lastIndex</code> value, then the browser is going to force itself into an infinite loop &#8211; which I think is a bug.</p>
<p>It works a bit like the following (the original code example came from Steven&#8217;s site but I&#8217;ve changed it to suit my own needs and added in my own comments to hopefully explain sufficiently what&#8217;s happening)&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> regex <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^/gm</span><span style="color: #339933;">,</span>
    subject <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;A<span style="color: #000099; font-weight: bold;">\n</span>B<span style="color: #000099; font-weight: bold;">\n</span>C&quot;</span><span style="color: #339933;">,</span>
    match<span style="color: #339933;">,</span>
    endPositions <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>match <span style="color: #339933;">=</span> regex.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/*
    As each match is a 'zero-width' match, an infinite loop occurs.
    i.e. the lastIndex value for each match is the same as the match index value
&nbsp;
    The reason we have an infinite loop happening is because each time .exec() is run it will run from the same starting position.
    e.g. a match is found at position 0, so the lastIndex is set to 0
&nbsp;
    So when the next match is attempted the regex starts from the lastIndex position of 0, and again a match is found at 0 (ad infinitum)
&nbsp;
    This is as per the official ECMAScript standard
    Only IE is unaffected as it doesn't follow the standard, but for once that's a good thing!
    */</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// So to avoid an infinite loop...</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>regex.<span style="color: #660066;">lastIndex</span> <span style="color: #339933;">===</span> match.<span style="color: #660066;">index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Let's see where the infinite loops would occur</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Increment lastIndex: &quot;</span> <span style="color: #339933;">+</span> regex.<span style="color: #660066;">lastIndex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 0, 2, 4</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Avoid an infinite loop by incrementing the lastIndex value by 1</span>
        <span style="color: #006600; font-style: italic;">// This is as per most other regex flavours and is also how other JavaScript methods behave? </span>
        <span style="color: #006600; font-style: italic;">// Again suggesting that the .exec() method was mistakenly defined as working the way it does.</span>
        regex.<span style="color: #660066;">lastIndex</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    endPositions.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>regex.<span style="color: #660066;">lastIndex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Show each character where a match was found</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>endPositions.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Match was found at character: &quot;</span> <span style="color: #339933;">+</span> subject.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>endPositions<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// A (position=0), B (position=2), C (position=4)</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Display lastIndex value for each match</span>
<span style="color: #006600; font-style: italic;">// Notice each value is 1 position ahead of where the match was found</span>
<span style="color: #006600; font-style: italic;">// This is to stop browsers (such as Firefox) getting stuck in an infinite loop when the first match is a zero-width match</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;lastIndex values after being incremented: &quot;</span> <span style="color: #339933;">+</span> endPositions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>You can see a working example on jsFiddle here: <a href="http://www.jsfiddle.net/integralist/N2xSb/">http://www.jsfiddle.net/integralist/N2xSb/</a>.</p>
<p>It&#8217;s a bit of a sticky issue and one that you shouldn&#8217;t see too much of really but is definitely worth being aware of so you know how to work around it if need be.</p>
<p>Feel free to share your thoughts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/javascripts-regular-expression-exec-lastindex-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression to open external links in popup window</title>
		<link>http://www.integralist.co.uk/javascript/regular-expression-to-open-external-links-in-popup-window/</link>
		<comments>http://www.integralist.co.uk/javascript/regular-expression-to-open-external-links-in-popup-window/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 10:13:11 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=415</guid>
		<description><![CDATA[This is a quick post to show you how to open up external website links within a pop-up window (without adding extra non semantic mark-up to your HTML code). In the past I&#8217;ve heard a lot of people talk about adding either custom attributes or using existing attributes such as rel as a hook for [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick post to show you how to open up external website links within a pop-up window (without adding extra non semantic mark-up to your HTML code). In the past I&#8217;ve heard a lot of people talk about adding either custom attributes or using existing attributes such as <code>rel</code> as a hook for your JavaScript code to find links that should open in a pop-up window. I disagree, and suggest using Regular Expressions (regexes) along with some procedural code to help figure this out for you (it will save you the time and hassle of adding this extra mark-up yourself).</p>
<p><span id="more-415"></span></p>
<p>This solution is an updated version. The previous version was recklessly using <a href="http://www.regular-expressions.info/brackets.html">capture groups</a> when really they should have been using non-capturing groups (as the brackets in the following solution are solely for applying <a href="http://www.regular-expressions.info/repeat.html">quantifiers</a> and <a href="http://www.regular-expressions.info/optional.html">optional items</a>). The reason you should use capturing groups sparingly is to do with performance and saving the regex engine from having to remember content within the capturing groups.</p>
<p>The <strong>Regular Expression</strong> checks for things like whether the URL is an SSL protected URL and uses a <a href="http://www.regular-expressions.info/lookaround.html">negative look ahead assertion</a> to make sure that it doesn&#8217;t incorrectly match URL&#8217;s that *appear* to be external (e.g. they start with a http) but in fact actually are links to the current website URL (this happens a lot with Content Management Systems where a user will copy and paste the full URL to one of their own pages).</p>
<h2>The Regex Solution</h2>
<pre class="regex">^http(?:s)?:\/\/(?!(?:www.)?integralist)</pre>
<h2>Example Code</h2>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * The Integralist global namespace object.
 *
 * @class Integralist
 * @singleton
 * @static
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> Integralist<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Constructor</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Augment the Integralist class so it includes a method
 * which finds all &lt;a&gt; elements that link to an external website
 * and sets them to open in a popup window
 */</span>
Integralist.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">external</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Required to keep scope within the following Closure</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'location=yes,resizable=yes,width='</span> <span style="color: #339933;">+</span> screen.<span style="color: #660066;">availWidth</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">',height='</span> <span style="color: #339933;">+</span> screen.<span style="color: #660066;">availHeight</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">',scrollbars=1,left=0,top=0'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">popup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> node.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> <span style="color: #3366CC;">'external'</span> <span style="color: #339933;">,</span> that.<span style="color: #660066;">settings</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Private variable to store HTMLCollection of all &lt;/a&gt;&lt;a&gt; elements</span>
		 len <span style="color: #339933;">=</span> a.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Store array length in variable</span>
		 pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^http(?:s)?:\/\/(?!(?:www.)?integralist)/</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// RegExp pattern to match any external URL's but not the current website</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Loop through the array checking for any A elements that link to an external URL</span>
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>len<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>len<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pattern.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			a<span style="color: #009900;">&#91;</span>len<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">popup</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>len<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Create a singleton of the Integralist Class</span>
<span style="color: #003366; font-weight: bold;">var</span> Integralist <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Integralist<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Trigger 'external' method</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> Integralist.<span style="color: #660066;">external</span><span style="color: #339933;">;</span></pre></div></div>
<h2 id="update1">Update 1</h2>
<p>Tweaked the regex again to make it simpler. Decided using a character class for just a single dot character was pointless, might as well just escape the dot. Also got rid of the non-capturing group around the s in https and just used an optional token ? on its own as again it was pointless wrapping a single character in a non-capturing group.</p>
<pre class="regex">^https?:\/\/(?!(?:www\.)?integralist)</pre>
<h2 id="update2">Update 2</h2>
<p>I&#8217;ve ended up tweaking the regex again to take into account files that don&#8217;t start with http but should still open in a pop-up window. For example if a website links to a self hosted PDF file it might store it in the following directory path <code>Assets/Documents/MyFile.pdf</code>. </p>
<p>First thing I did was add a case-insensitive modifier flag (which I simply forgot last time). </p>
<p>Now to work around the issue of linking to internal documents that should open in a pop-up I place a non-capturing group after the opening anchor <code>^</code>. Within this group it has a character class that allows a period <code>.</code> and a forward slash <code>/</code> and uses a quantifier <code>+</code> so it matches 1 or more times. I then make the whole group optional.</p>
<p>Next, we take the original regex and wrap it in a non-capturing group and place it after the preceding code. Inside the non-capturing group, at the start we add the name of the directory we are looking for (in this case I just need to look for the word &#8220;Assets&#8221; and I know it&#8217;s going to link to a document and not a HTML file). I then put in the alternator meta-character so it will look for &#8220;Assets&#8221; and if it can&#8217;t find it the regex engine will backtrack to this point and try the rest of the regex which we&#8217;ve already discussed.</p>
<p>So the final regex looks like this&#8230;</p>
<pre class="regex">^(?:[.\/]+)?(?:Assets|https?:\/\/(?!(?:www\.)?integralist))</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/regular-expression-to-open-external-links-in-popup-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression Header Text Replacement</title>
		<link>http://www.integralist.co.uk/javascript/regular-expression-header-text-replacement/</link>
		<comments>http://www.integralist.co.uk/javascript/regular-expression-header-text-replacement/#comments</comments>
		<pubDate>Thu, 27 May 2010 20:37:18 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=380</guid>
		<description><![CDATA[This is a post about some of my findings whilst trying to use Regular Expressions (Regex) to solve some problematic string issues. Along the way I learned something new about JavaScript&#8217;s text replacement methods that I didn&#8217;t know before and although it was very interesting to finally figure out, I was still slightly annoyed that [...]]]></description>
			<content:encoded><![CDATA[<p>This is a post about some of my findings whilst trying to use Regular Expressions (Regex) to solve some problematic string issues. Along the way I learned something new about JavaScript&#8217;s text replacement methods that I didn&#8217;t know before and although it was very interesting to finally figure out, I was still slightly annoyed that it didn&#8217;t feature more prominently in more leading JavaScript books on the relevant sections of Regexes and text replacement using Regexes.</p>
<p><span id="more-380"></span></p>
<p>I recently had a job where a <code>&lt;h1&gt;</code> tag needed to be split onto two or more lines. The reason for it to be split up onto multiple lines was to make it fit the creative our designers had come up with (which was an old skool style 80&#8242;s retro looking banner &#8211; not the easiest thing in the world to pull off basically).</p>
<p>The code for the header initially looked like this&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
	one two<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	three<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	four<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span></pre></div></div>
<p>But the next issue I came across was that to be able to implement this crazy design and have it be cross-browser compatible I needed to force in a <code>&amp;nbsp;</code> (non-breaking space) before and after the line breaks, AND wrap the whole thing in a <code>&lt;div&gt;</code> such as this&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;h1block&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
		one two<span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span>
		three<span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span>
		four<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>
<p>But the biggest issue was that the site and all its text was controlled by a Content Management System (CMS), so there was no way I could control this sort of strict styling.</p>
<p>Here is where Regular Expressions came to the rescue.</p>
<p>Now, to my mistake I tried to implement this using one Regex. I almost got it but then hit a brick wall&#8230;</p>
<h2>Original Regex</h2>
<pre class="regex">(&lt;h1&gt;)(?:([^<]*?)(&lt;br /&gt;))*?([^<]*?)(&lt;/h1&gt;)</pre>
<h2>Original Replacement Text</h2>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;h1block&quot;</span><span style="color: #339933;">&gt;</span>$<span style="color: #CC0000;">1</span>$<span style="color: #CC0000;">2</span><span style="color: #339933;">&amp;</span>nbsp<span style="color: #339933;">;</span>$<span style="color: #CC0000;">3</span><span style="color: #339933;">&amp;</span>nbsp<span style="color: #339933;">;</span>$<span style="color: #CC0000;">4</span>$<span style="color: #CC0000;">5</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>
<p>But the problem I had was that the replacement only remembers the 2nd before last line &#8220;three&#8221;, and not &#8220;one two&#8221;.</p>
<p>Regex Guru &#8220;Jan Goyvaerts&#8221; then pointed me in the right direction and suggested that I not over complicate things and instead split my single monolithic regex into two.</p>
<p>Although the following solution is actually written in JavaScript I eventually got our server-side developer to implement a similar solution using PHP&#8217;s <code>preg_replace</code> (the Regex itself didn&#8217;t need to change, just the syntax sugar around it).</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> subject <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;h1&gt; <span style="color: #000099; font-weight: bold;">\</span>
    one two&lt;br /&gt; <span style="color: #000099; font-weight: bold;">\</span>
    three&lt;br /&gt; <span style="color: #000099; font-weight: bold;">\</span>
    four <span style="color: #000099; font-weight: bold;">\</span>
    &lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> content<span style="color: #339933;">,</span>
    pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;h1&gt;([\S\s]*)&lt;\/h1&gt;/</span><span style="color: #339933;">,</span>
    match <span style="color: #339933;">=</span> pattern.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>match <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    content<span style="color: #339933;">=</span> match<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;br\s*\/?&gt;/g</span><span style="color: #339933;">;</span>
    final <span style="color: #339933;">=</span> content.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>pattern<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&amp;nbsp;$&amp;&amp;nbsp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// $&amp; is a reference to the matched content</span>
&nbsp;
    document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;h1block&quot;&gt;&lt;h1&gt;'</span> <span style="color: #339933;">+</span> final <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h1&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<h2>Working Example</h2>
<p>You can also go direct to the example on jsFiddle: <a href="http://jsfiddle.net/Jrhdb/">http://jsfiddle.net/Jrhdb/</a></p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/Jrhdb/embedded/"></iframe></p>
<h2>Final Regex Solution(s)</h2>
<p>Here we match a literal <code>&lt;h1&gt;</code> followed by any character zero or more times, which itself is wrapped in a &#8216;capture group&#8217; so we can refer back to the content of the group later on, followed lastly by a literal <code>&lt;/h1&gt;</code>.</p>
<pre class="regex">&lt;h1&gt;([\S\s]*)<\/h1></pre>
<p>Then, in the above JavaScript example we take the content within the capture group and look for any <br /> tags, as per the regex which follows&#8230;</p>
<pre class="regex">&lt;br\s*\/?></pre>
<p>&#8230;we then use JavaScript&#8217;s <code>replace()</code> method to insert the non-breaking spaces (&amp;nbsp).</p>
<p>And finally we use simple string concatenation to wrap the appropriate <code>div</code> around the entire match.</p>
<h2>Some Additional Regex Text Replacement Surprises</h2>
<p>Through this process I actually learned about JavaScript&#8217;s special tokens for reinserting the overall regex match which is <code>$&#038;</code>. There is also <code>$‘</code> which inserts the portion of string that <em>precedes</em> the matched substring, and there is <code>$’</code> which inserts the portion of string that <em>follows</em> the matched substring.</p>
<p>None of these three special tokens I knew about until now! I only ever knew about numbered back references such as <code>$1, $2, $3...</code> and I&#8217;m surprised not to see it in more JavaScript books (up till now I&#8217;ve only seen <code>$&#038;</code> mentioned in the excellent book &#8220;Regular Expressions Cookbook&#8221;, and have been told all three are briefly mentioned in the reference section of &#8220;JavaScript: The Definitive Guide&#8221;?).</p>
<p>Either way, useful knowledge to have.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/regular-expression-header-text-replacement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Interfaces in JavaScript</title>
		<link>http://www.integralist.co.uk/javascript/implementing-interfaces-in-javascript/</link>
		<comments>http://www.integralist.co.uk/javascript/implementing-interfaces-in-javascript/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 22:21:13 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Interface]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=339</guid>
		<description><![CDATA[In Object-Oriented languages an interface defines a set of methods which a Class must include in order to implement the interface (otherwise, if the Class is missing the required methods, the code will fail and the interface will throw an error). Interfaces are useful for making sure developers use the correct implementation of an API. [...]]]></description>
			<content:encoded><![CDATA[<p>In Object-Oriented languages an interface defines a set of methods which a Class must include in order to implement the interface (otherwise, if the Class is missing the required methods, the code will fail and the interface will throw an error).</p>
<p>Interfaces are useful for making sure developers use the correct implementation of an API.</p>
<p>In JavaScript there are no true &#8216;classic&#8217; Object-Oriented features, but through clever usage of the language you can emulate an Interface for use with a JavaScript API.</p>
<p><span id="more-339"></span></p>
<p>First thing you want to do is include the following JavaScript file (<code>Interface.js</code>) into your HTML:</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * Constructor that creates a new Interface object for checking a function implements the required methods.
 * @param objectName | String | the instance name of the Interface
 * @param methods | Array | methods that should be implemented by the relevant function
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> Interface <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>objectName<span style="color: #339933;">,</span> methods<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Check that the right amount of arguments are provided</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Interface constructor called with &quot;</span> <span style="color: #339933;">+</span> arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;arguments, but expected exactly 2.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Create the public properties</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> objectName<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">methods</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Loop through provided arguments and add them to the 'methods' array</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> methods.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Check the method name provided is written as a String</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> methods<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'string'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Interface constructor expects method names to be &quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;passed in as a string.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #006600; font-style: italic;">// If all is as required then add the provided method name to the method array</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">methods</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
 * Adds a static method to the 'Interface' constructor
 * @param object | Object Literal | an object literal containing methods that should be implemented
 */</span>
Interface.<span style="color: #660066;">ensureImplements</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Check that the right amount of arguments are provided</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Interface.ensureImplements was called with &quot;</span> <span style="color: #339933;">+</span> arguments.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;arguments, but expected at least 2.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Loop through provided arguments (notice the loop starts at the second argument)</span>
	<span style="color: #006600; font-style: italic;">// We start with the second argument on purpose so we miss the data object (whose methods we are checking exist)</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Check the object provided as an argument is an instance of the 'Interface' class</span>
		<span style="color: #003366; font-weight: bold;">var</span> interface <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>interface.<span style="color: #660066;">constructor</span> <span style="color: #339933;">!==</span> Interface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Interface.ensureImplements expects the second argument to be an instance of the 'Interface' constructor.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Otherwise if the provided argument IS an instance of the Interface class then</span>
		<span style="color: #006600; font-style: italic;">// loop through provided arguments (object) and check they implement the required methods</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> methodsLen <span style="color: #339933;">=</span> interface.<span style="color: #660066;">methods</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> methodsLen<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> method <span style="color: #339933;">=</span> interface.<span style="color: #660066;">methods</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Check method name exists and that it is a function (e.g. test[getTheDate]) </span>
			<span style="color: #006600; font-style: italic;">// if false is returned from either check then throw an error</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>object<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span> object<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;This Class does not implement the '&quot;</span> <span style="color: #339933;">+</span> interface.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' interface correctly. The method '&quot;</span> <span style="color: #339933;">+</span> method <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' was not found.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>
<p>Next, in the HTML file we have an inline script that creates a new Interface instance and then passes it the methods we are expecting. The user of the API would need to implement an object literal of all the methods the interface requires (note the following code could be in a separate SCRIPT file as long as it is run after the above script):</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// We pass into the Interface the name of the current Interface instance, </span>
<span style="color: #006600; font-style: italic;">// followed by an Array of the methods we are expecting to find</span>
<span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Interface<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'details'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> properties <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Mark McDonnell&quot;</span><span style="color: #339933;">,</span>
	actions<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		details<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;I am &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">age</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; years old.&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		age<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>birthdate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> dob <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>birthdate<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				today <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				ms <span style="color: #339933;">=</span> today.<span style="color: #660066;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> dob.<span style="color: #660066;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				minutes <span style="color: #339933;">=</span> ms <span style="color: #009966; font-style: italic;">/ 1000 /</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">,</span>
				hours <span style="color: #339933;">=</span> minutes <span style="color: #339933;">/</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">,</span>
				days <span style="color: #339933;">=</span> hours <span style="color: #339933;">/</span> <span style="color: #CC0000;">24</span><span style="color: #339933;">,</span>
				years <span style="color: #339933;">=</span> days <span style="color: #339933;">/</span> <span style="color: #CC0000;">365</span><span style="color: #339933;">,</span>
				age <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>years<span style="color: #009900;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">return</span> age<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;1981 08 30&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Create a Person constructor that will implement the above properties/methods</span>
<span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Pass in the methods we are expecting, </span>
	<span style="color: #006600; font-style: italic;">// followed by the name of the Interface instance that we're checking against</span>
	Interface.<span style="color: #660066;">ensureImplements</span><span style="color: #009900;">&#40;</span>config.<span style="color: #660066;">actions</span><span style="color: #339933;">,</span> test<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> config.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">methods</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">actions</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Create a new instance of the Person constructor...</span>
<span style="color: #003366; font-weight: bold;">var</span> me <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span>properties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// ...and make sure the methods are working</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>me.<span style="color: #660066;">methods</span>.<span style="color: #660066;">age</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>me.<span style="color: #660066;">method</span>.<span style="color: #660066;">details</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/implementing-interfaces-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression Basics</title>
		<link>http://www.integralist.co.uk/javascript/regular-expression-basics/</link>
		<comments>http://www.integralist.co.uk/javascript/regular-expression-basics/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 21:13:58 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[RegEx]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=322</guid>
		<description><![CDATA[Regular Expressions are a powerful utility within the JavaScript language. Essentially it allows you to search and manipulate strings of text with both simple and complex patterns. For example, you&#8217;ll normally find a lot of form validation is done using Regular Expressions (see: http://codecanyon.net/item/validate/54689). Regular Expressions are available in one form or another in most [...]]]></description>
			<content:encoded><![CDATA[<p>Regular Expressions are a powerful utility within the JavaScript language. Essentially it allows you to search and manipulate strings of text with both simple and complex patterns.</p>
<p><span id="more-322"></span></p>
<p>For example, you&#8217;ll normally find a lot of form validation is done using Regular Expressions (see: <a href="http://codecanyon.net/item/validate/54689" rel="external">http://codecanyon.net/item/validate/54689</a>).</p>
<p>Regular Expressions are available in one form or another in most programming languages. One minor point to make here is that although most programming languages include Regular Expressions, they can differ in implementation from language to language. So you may find the particular Regular Expression feature you want to use is not supported in your language of choice.</p>
<p>JavaScript for example implements the Perl 5 syntax (and as such doesn&#8217;t support certain features such as &#8216;look-behind&#8217; constructs).</p>
<p>When writing a Regular Expression you will start with the string you want to match a pattern against. You&#8217;ll write your Regular Expression &#8216;pattern&#8217; and will then assign one (or more) &#8216;modifiers&#8217; to the Regular Expression object which will effect how the pattern is run.</p>
<p>For example if you wanted to search a string for the word &#8220;JavaScript&#8221; your pattern would be /JavaScript/, but if your string had that word only in lowercase (i.e. &#8220;I love javascript&#8221;) then the pattern match would fail. So you would assign a &#8220;case insensitive&#8221; modifier to let the Regular Expression engine know that it is free to find matches as either &#8220;JavaScript&#8221; or &#8220;javascript&#8221; or even &#8220;JAVASCRIPT&#8221;.</p>
<p>There are three types of modifiers:</p>
<ul>
<li><code>i</code>, &#8211; “ignore case” &#8211; the case (uppercase/lowercase) of all letters within the string will be ignored during testing.</li>
<li><code>g</code>, &#8211; “global search” &#8211; the search is carried out across the entire string, regardless of whether a match has already been found.</li>
<li><code>m</code>, &#8211; “multiline search” &#8211; the regular expression will match over multiple lines.</li>
</ul>
<p>In JavaScript there are essentially three main methods for handling Regular Expressions:</p>
<ol>
<li><code>String.match</code> (returns an Array of matches)</li>
<li><code>RegExp.exec</code> (returns an Array of matches)</li>
<li><code>RegExp.test</code> (returns a Boolean true/false)</li>
</ol>
<p>The only difference between <code>String.match</code> and <code>RegExp.exec</code> is that the latter will return capture groups plus the first match (if a global modifier has been used), where as the former won&#8217;t return any capture groups only the full matches. Note: a capture group is when you wrap a certain set of characters in parenthesis, any text in () will then be remembered after the Regular Expression has run and can be replaced or moved (see below for an example of this).</p>
<p>The <code>RegExp.test</code> method is probably the most useful as far as validation testing is concerned as the majority of the time you just want to check if a specific value is present.</p>
<p>I&#8217;ve included some extremely basic examples below as a way to gently introduce newcomers to the terminology of Regular Expressions. A great tool for testing Regular Expressions can be found here: <a href="http://gskinner.com/RegExr/" rel="external">http://gskinner.com/RegExr/</a> (I highly recommend you try it). </p>
<p>In my examples I&#8217;ve used the <code>String.match</code> method of matching patterns and I&#8217;ve also used the &#8216;regexp literal&#8217; form of creating Regular Expression (e.g. /my-regexp-pattern/) but, there is also the <code>RegExp()</code> constructor which allows you to create Regular Expression objects. </p>
<p>The only benefit of using the RegExp constructor is if you need to search for patterns dynamically (i.e. with unknown data. For example, such as at runtime when the user specifies a word they want to search for).</p>
<h2>Regular Expression Basic Examples&#8230;</h2>
<p>NOTE: Two of the examples use what is called a &#8216;look-ahead&#8217; construct (I demonstrate a positive and negative variation of the look-ahead) and although Regular Expressions do have a &#8216;look-behind&#8217; construct, the current version of JavaScript doesn&#8217;t implement it. There are ways to mimic it but that is outside the scope of this article so I&#8217;ll refer you to here: <a href="http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript" rel="external">http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript</a></p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	<span style="color: #003366; font-weight: bold;">var</span> string <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Here is some text about JavaScript (also known as JScript). It contains multiple references to jscript/javascript because this article is all about JAVASCRIPT.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [JavaScript]</span>
	<span style="color: #006600; font-style: italic;">// Although there are multiple references to the word &quot;javascript&quot; I've not included the &quot;global&quot; modifier so it has only returned the first match</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/javascript/i</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [JavaScript,javascript,JAVASCRIPT]</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/javascript/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [&quot;Java&quot;, &quot;java&quot;, &quot;JAVA&quot;]</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/java/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [a,J,a,v,a,a,a,J,a,j,j,a,v,a,a,a,a,a,J,A,V,A]</span>
	<span style="color: #006600; font-style: italic;">// Match any single character in the set (e.g. /defen[cs]e/ would matche either defense or defence).</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[java]/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [e,r,e,i,s,s,o,m,e,t,e,x,t,a,b,o,u,t,a,v,a,c,r,i,p,t,a,l,s,o,k,n,o,w,n,a,s,c,r,i,p,t,t,c,o,n,t,a,i,n,s,m,u,l,t,i,p,l,e,r,e,f,e,r,e,n,c,e,s,t,o,j,s,c,r,i,p,t,j,a,v,a,s,c,r,i,p,t,b,e,c,a,u,s,e,t,h,i,s,a,r,t,i,c,l,e,i,s,a,l,l,a,b,o,u,t] </span>
	<span style="color: #006600; font-style: italic;">// Match any single character in the set (e.g. /defen[cs]e/ would matche either defense or defence).</span>
	<span style="color: #006600; font-style: italic;">// Notice the capital letters are missing from the result set.</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[a-z]/g</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [H, , , , , ,J,S, ,(, , , ,J,S,),., ,I, , , , , ,/, , , , , , , ,J,A,V,A,S,C,R,I,P,T,.] </span>
	<span style="color: #006600; font-style: italic;">// Match any single character that is not in the set.</span>
	<span style="color: #006600; font-style: italic;">// Matches anything that isn't a capital (includes spaces inbetween words)</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^a-z]/g</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [J,J,j,j,J]</span>
	<span style="color: #006600; font-style: italic;">// Alternation. Equivalent of &quot;or&quot;. Matches the full expression before or after the |.</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/J|j/g</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [J,j,J] </span>
	<span style="color: #006600; font-style: italic;">// Positive lookahead. Matches a group after your main expression without including it in the result.</span>
	<span style="color: #006600; font-style: italic;">// Matches j that have &quot;ava&quot; following them (so jscript or JScript don't match)</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/J(?=ava)/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [J,j] </span>
	<span style="color: #006600; font-style: italic;">// Negative lookahead. Specifies a group that can not match after your main expression (ie. if it matches, the result is discarded).</span>
	<span style="color: #006600; font-style: italic;">// Matches j that HASN'T got &quot;ava&quot; following them (so jscript &amp; JScript return a match)</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/J(?!ava)/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [ , , , , , , , , , , , , , , , , , , , , , ]</span>
	<span style="color: #006600; font-style: italic;">// \s matches any whitespace character (spaces, tabs, line breaks).</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s/g</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [JavaScript,javascript,JAVASCRIPT] </span>
	<span style="color: #006600; font-style: italic;">// \b matches a word boundary position such as whitespace or the beginning or end of the string.</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/J.{6}ipt\b/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: [JavaScript,javascript,JAVASCRIPT]</span>
	<span style="color: #006600; font-style: italic;">// {} means to match a pattern a specified number of times (in this case any character 6 times after &quot;J&quot; and ending in &quot;ipt&quot;)</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/J.{6}ipt/ig</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// returns: &quot;Here is some text about JJavaScript (also known as JScript). It contains multiple references to jscript/jjavaScript because this article is all about JJavaScript.&quot;</span>
	<span style="color: #006600; font-style: italic;">// Groups multiple tokens together. This allows you to apply quantifiers to the full group. This creates a capturing group.</span>
	<span style="color: #006600; font-style: italic;">// Notice the double &quot;JJ&quot; where we've captured the letter j and then replace it with itself twice.</span>
	<span style="color: #006600; font-style: italic;">// If you capture more than one item then all subsequent capture groups are referenced by number incrementally (e.g. $2, $3, $4 etc.)</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(j)avascript/ig</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'$1$1avaScript'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/regular-expression-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mixin Classes as a way to reuse JavaScript code</title>
		<link>http://www.integralist.co.uk/javascript/mixin-classes-as-a-way-to-reuse-javascript-code/</link>
		<comments>http://www.integralist.co.uk/javascript/mixin-classes-as-a-way-to-reuse-javascript-code/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 23:12:27 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[Mixin]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Prototype]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=291</guid>
		<description><![CDATA[This is a useful example of how you can augment a Class to have a particular method but not by using strict inheritance or by duplicating the relevant code for each Class you have. I came across this technique (known as &#8220;Mixin Classes&#8221;) while reading the book &#8220;Pro JavaScript Design Patterns&#8221;. In the following code [...]]]></description>
			<content:encoded><![CDATA[<p>This is a useful example of how you can augment a Class to have a particular method but not by using strict inheritance or by duplicating the relevant code for each Class you have. I came across this technique (known as &#8220;Mixin Classes&#8221;) while reading the book &#8220;Pro JavaScript Design Patterns&#8221;.</p>
<p><span id="more-291"></span></p>
<p>In the following code example we have a &#8216;Person&#8217; Class which has no methods.<br />
We also have another Class called &#8216;Mixin&#8217; which has three methods.<br />
We will augment the &#8216;Person&#8217; Class so it has access to two of the three methods within the &#8216;Mixin&#8217; Class.</p>
<p>I&#8217;ve not put any further details as the code should be self-explanatory. Otherwise I suggest you purchase the book &#8220;Pro JavaScript Design Patterns&#8221; to help yourself understand what the code does and why.</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Person Class */</span>
<span style="color: #003366; font-weight: bold;">var</span> Person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> settings.<span style="color: #000066;">name</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">'no name given'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">age</span> <span style="color: #339933;">=</span> settings.<span style="color: #660066;">age</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">'no age given'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/* Mixin Class */</span>
<span style="color: #003366; font-weight: bold;">var</span> Mixin <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Mixin.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	doSomething1<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'do something 1!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	doSomething2<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'do something 2!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	doSomething3<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'do something 3!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Function to augment an existing class with a method(s) from another class</span>
<span style="color: #003366; font-weight: bold;">function</span> augment<span style="color: #009900;">&#40;</span>receivingClass<span style="color: #339933;">,</span> givingClass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Only give certain methods</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			receivingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> givingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #006600; font-style: italic;">// Give all methods found</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>methodName <span style="color: #000066; font-weight: bold;">in</span> givingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// A quick check to make sure the receiving class doesn't already have a method of the same name as what's currently being processed</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>receivingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>methodName<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				receivingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>methodName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> givingClass.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#91;</span>methodName<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Augment the Person class to have the methods 'doSomething1' and 'doSomething3'</span>
<span style="color: #006600; font-style: italic;">/*
	We can also do...
		augment(Person, Mixin);
	...so the 'Person' class receives ALL methods within 'Mixin'.
*/</span>
augment<span style="color: #009900;">&#40;</span>Person<span style="color: #339933;">,</span> Mixin<span style="color: #339933;">,</span> <span style="color: #3366CC;">'doSomething1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'doSomething3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Create a new Person</span>
<span style="color: #003366; font-weight: bold;">var</span> employee <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'John Smith'</span><span style="color: #339933;">,</span> age<span style="color: #339933;">:</span><span style="color: #CC0000;">27</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Test to make sure we have access to the relevant methods</span>
employee.<span style="color: #660066;">doSomething1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
employee.<span style="color: #660066;">doSomething3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Just to prove we aren't copying all the methods from the 'Mixin' Class...</span>
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	employee.<span style="color: #660066;">doSomething2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/mixin-classes-as-a-way-to-reuse-javascript-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grab HTML fragment with AJAX</title>
		<link>http://www.integralist.co.uk/javascript/grab-html-fragment-with-ajax/</link>
		<comments>http://www.integralist.co.uk/javascript/grab-html-fragment-with-ajax/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 16:13:37 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=275</guid>
		<description><![CDATA[If you&#8217;re new to AJAX you may be wondering how you can pull in just a single element from another HTML page (as calling an HTML page with AJAX pulls the entire HTML content in as a String). It has also been explained to me that the particular solution I&#8217;m going to discuss here is [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re new to AJAX you may be wondering how you can pull in just a single element from another HTML page (as calling an HTML page with AJAX pulls the entire HTML content in as a String).</p>
<p>It has also been explained to me that the particular solution I&#8217;m going to discuss here is the same technique used by the jQuery library.</p>
<p><span id="more-275"></span></p>
<p>I&#8217;m not going to run through how to actually write an AJAX method as that is outside the scope of this post, so feel free to use your own home baked JavaScript code, or use any one of the numerous JavaScript libraries as they all include their own variation of a cross-browser AJAX call.</p>
<p>With most AJAX scripts you&#8217;ll also specify a callback function which will run once the requested data has been retrieved. In my case this looks something like this&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">Remote</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Test.html&quot;</span><span style="color: #339933;">,</span>
	callback<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Do something with the response object</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>Once my AJAX function has successfully retrieved the data, it runs the callback function which then processes the data and grabs the element we&#8217;re interested in. </p>
<p>Within the callback function we first need to create a new element and then set the elements innerHTML to the response data (which is returned as a String). </p>
<p>We then grab all the elements we&#8217;re interested in (for this example I&#8217;m specifically looking for a DIV element that has an ID value of &#8220;content&#8221;) and loop through them until we find the relevant node&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Load the contents of the HTML file 'Test.html'</span>
$.<span style="color: #660066;">Remote</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Test.html&quot;</span><span style="color: #339933;">,</span>
	callback<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Now we have the HTML as a String </span>
		<span style="color: #006600; font-style: italic;">// lets parse it into a DOM node</span>
		<span style="color: #006600; font-style: italic;">// so we can then locate an element </span>
		<span style="color: #006600; font-style: italic;">// within the HTML we want to pull in</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Create an empty DIV which wont be used for anything</span>
		<span style="color: #006600; font-style: italic;">// other than storing the HTML response</span>
		<span style="color: #003366; font-weight: bold;">var</span> tempNode <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Set the DIV's innerHTML to the HTML String we've received</span>
		tempNode.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">text</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Grab a reference to all DIV's in the 'Test.html' page</span>
		<span style="color: #003366; font-weight: bold;">var</span> nodes <span style="color: #339933;">=</span> tempNode.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Loop through each DIV found...</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>nodes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// ...looking for the DIV we want</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>nodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;content&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Insert the found element into the page</span>
				document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>nodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Clean-up</span>
				<span style="color: #003366; font-weight: bold;">var</span> tempNode <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> nodes <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Break the loop now we have found the element we want</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>Simple as that!</p>
<p>Obviously you could clean this up a lot and write a function that takes in as an argument the element you&#8217;re looking for as well as a class name/id and then have it return the relevant content, but for this example I&#8217;m just using my own box standard AJAX code and searching through the response data from within my callback function.</p>
<p>Remember also that this is just example code and is far from perfect, but hopefully it has at least clarified the process of how to grab an HTML fragment from a separate HTML page using AJAX.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/javascript/grab-html-fragment-with-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cross Browser JavaScript DOMContentLoaded</title>
		<link>http://www.integralist.co.uk/html/cross-browser-javascript-domcontentloaded/</link>
		<comments>http://www.integralist.co.uk/html/cross-browser-javascript-domcontentloaded/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 23:17:44 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOMContentLoaded]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=250</guid>
		<description><![CDATA[This post shows you how you can call your JavaScript functions once the Document Object Model is ready (which is a lot faster than waiting for the page to finishing loading when using window.onload). The following code is an amalgamation of the work of Jesse Skinner and Byron McGregor. It keeps track of the methods [...]]]></description>
			<content:encoded><![CDATA[<p>This post shows you how you can call your <strong class="keyword">JavaScript</strong> functions once the Document Object Model is ready (which is a lot faster than waiting for the page to finishing loading when using <code>window.onload</code>).</p>
<p>The following code is an amalgamation of the work of Jesse Skinner and Byron McGregor. It keeps track of the methods passed into the <code><strong class="keyword">addDOMLoadEvent</strong></code> function and supports all modern browsers such as Firefox, Chrome, Opera, Safari (higher than 525) and falls back to <code>window.onload</code> for those browsers that fail all the other checks.</p>
<p>I&#8217;ve also updated the code to take into account one particular issue I found with previous versions when using the doScroll method of targeting Internet Explorer which would sometimes return &#8220;unavailable&#8221;, as well as fixing a Firefox error when hitting the Safari targeted code.</p>
<p><span id="more-250"></span></p>
<p>To start with lets take a look at the HTML for my test page:</p>
<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JavaScript. onDOMLoad<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html;charset=utf-8&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
	*
	{ font-family: Arial, Helvetica, sans-serif; }
&nbsp;
	li
	{ margin:25px; }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>addDOMLoadEvent Demo Page<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Supports Mozilla/Opera/Chrome/Safari &gt; 525/IE6+<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ol</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>JavaScript will add more LI elements once the DOM is loaded.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Large1.jpg&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span> (approx 1.5mb)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Large2.jpg&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span> (approx 1.5mb)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Large3.jpg&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span>&gt;</span> (approx 1.5mb)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ol</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;domready.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;library.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>
<p>The following code adds elements to the DOM while the images continue to download:</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> MYAPP <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		MYAPP.<span style="color: #660066;">addListElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Thanks, the DOM appears to be loaded!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	addFunction<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Create a closure</span>
		<span style="color: #006600; font-style: italic;">// so we can keep track of</span>
		<span style="color: #006600; font-style: italic;">// the number of times this method is called</span>
		<span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			counter<span style="color: #339933;">++;</span>
			MYAPP.<span style="color: #660066;">addListElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Some random text &quot;</span> <span style="color: #339933;">+</span> counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	addListElement<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> li <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		li.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ol <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ol.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
addDOMLoadEvent<span style="color: #009900;">&#40;</span>MYAPP.<span style="color: #660066;">init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addDOMLoadEvent<span style="color: #009900;">&#40;</span>MYAPP.<span style="color: #660066;">addFunction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addDOMLoadEvent<span style="color: #009900;">&#40;</span>MYAPP.<span style="color: #660066;">addFunction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addDOMLoadEvent<span style="color: #009900;">&#40;</span>MYAPP.<span style="color: #660066;">addFunction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addDOMLoadEvent<span style="color: #009900;">&#40;</span>MYAPP.<span style="color: #660066;">addFunction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addDOMLoadEvent<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	MYAPP.<span style="color: #660066;">addListElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MY FINAL METHOD CALL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>And now here is the final <code><strong class="keyword">addDOMLoadEvent</strong></code> code:</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> addDOMLoadEvent <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> load_events <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Stacks all functions sent to addDOMLoadEvent</span>
   	 load_timer<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Used as a timer for versions of Safari older than 525 and Internet Explorer</span>
       done<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Used to check when the DOM has completed loading</span>
       exec<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Used to execute each function in the load_events stack</span>
       old_onload<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Used as a fallback for older browsers that fail all other means of emulating DOMContentLoaded</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// The init method will be called when the DOM is loaded (or when the window.onload event is triggered).</span>
	<span style="color: #006600; font-style: italic;">// It lets the application know the DOM is ready by setting 'done' to true,</span>
	<span style="color: #006600; font-style: italic;">// then runs a quick clean up of the load_timer interval,</span>
	<span style="color: #006600; font-style: italic;">// and then executes all the functions in the load_events stack (in the order they were added).</span>
	<span style="color: #003366; font-weight: bold;">var</span> init <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		done <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		clearInterval<span style="color: #009900;">&#40;</span>load_timer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>exec <span style="color: #339933;">=</span> load_events.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			exec<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// We check to see if the DOM has already loaded.</span>
		<span style="color: #006600; font-style: italic;">// If it is then just run the function that's trying to be called and stop there.</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>done<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>load_events<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Standard compliant browsers (inc. Mozilla/Opera/Chrome/Safari)</span>
				document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DOMContentLoaded&quot;</span><span style="color: #339933;">,</span> init<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Internet Explorer</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">/**
			 * For full information see comment 201
			 * http://dean.edwards.name/weblog/2006/06/again/#comment335794
			 *
			 * Summing up the IE challenge:
			 * Initialization event behaviours and order vary greatly among different pages.
			 *
			 * Dean Edwards document.write of the deferred script has given problems on some pages (causing a consistent &amp;gt; 60 sec delay).
			 * Dean confirmed that document.readyState is unreliable.
			 * We have seen cases where document.readyState was “complete” while document.body was still null.
			 * And other cases where document.readyState was not complete until after all images on the page were loaded.
			 *
			 * The doScroll method has been seen to succeed while document.body is still null.
			 * And as already mentioned, document.body can be non-null prior to the DOM being available.
			 *
			 * One solution that so far has tested 100% OK is to combine a test for both document.body and success of doScroll.
			 * And this is the method we use as it has so far provided perfect DOMContentLoaded emulation on all pages tested.
			 * Sometimes the doScroll is not available and IE falls back to window.onload so I've used a timer to counter this.
			 *
			 * Note that we use IE's Conditional Compilation to target IE rather than browser sniffing
			 * http://msdn.microsoft.com/en-gb/library/121hztk3(VS.85).aspx
			 */</span>
&nbsp;
			<span style="color: #009966; font-style: italic;">/*@cc_on @*/</span>
			<span style="color: #006600; font-style: italic;">/*@if (@_win32)
				load_timer = setInterval(function()
				{
					if (document.body) {
						try {
							document.createElement('div').doScroll('left');
							clearInterval(load_timer);
							return init();
						} catch(e) {}
					}
				}, 10);
			@*/</span>
			<span style="color: #009966; font-style: italic;">/*@end @*/</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Safari (versions older than 525 which don't support the DOMContentLoaded event)</span>
			<span style="color: #006600; font-style: italic;">// Use a try-catch statement to make sure no errors (such as Firefox generates) are displayed.</span>
			<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">userAgent</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/AppleWebKit\/(\d+)/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #CC0000;">525</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					load_timer <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/loaded|complete/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// For other browsers fall back to calling init() on the window.onload event.</span>
			<span style="color: #006600; font-style: italic;">// But first store the original window.onload (just in case it was already set) and execute after init().</span>
			old_onload <span style="color: #339933;">=</span> window.<span style="color: #000066;">onload</span><span style="color: #339933;">;</span>
			window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Calling init() within the window.onload event wont affect modern browsers.</span>
				<span style="color: #006600; font-style: italic;">// The reason being the init() function has already been called.</span>
				<span style="color: #006600; font-style: italic;">// So the stack of waiting functions have been cleared out already.</span>
				init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;">// Run the original window.onload after init()</span>
				<span style="color: #006600; font-style: italic;">// as window.onload should run after DOMContentLoaded.</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>old_onload<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					old_onload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;window.onload is complete&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// We push the requested function into the stack array,</span>
		<span style="color: #006600; font-style: italic;">// which will be executed either once the DOM has loaded or the window.onload is called.</span>
		load_events.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>This seems to work pretty well for me.</p>
<p>I&#8217;m always interested to hear other peoples views on the <strong class="keyword">DOMContentLoaded</strong> dilemma so if you see anything that you would change then feel free to share.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/html/cross-browser-javascript-domcontentloaded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling an ActionScript function from JavaScript</title>
		<link>http://www.integralist.co.uk/actionscript/calling-an-actionscript-function-from-javascript/</link>
		<comments>http://www.integralist.co.uk/actionscript/calling-an-actionscript-function-from-javascript/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 13:36:45 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[Flash]]></category>
		<guid isPermaLink="false">http://www.integralist.co.uk/?p=214</guid>
		<description><![CDATA[If you need to access your Flash/ActionScript functions via JavaScript then the easiest way to do this is with the ExternalInterface API (click here to read the Adobe documentation). This post assumes ActionScript 3.0/Flash Player 9+ and for the purposes of this article I&#8217;ve avoided using certain best practices (such as inserting Flash content using [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to access your Flash/ActionScript functions via JavaScript then the easiest way to do this is with the ExternalInterface API (click here to read the Adobe documentation).</p>
<p><span id="more-214"></span></p>
<p>This post assumes <strong class="keyword">ActionScript 3.0</strong>/Flash Player 9+ and for the purposes of this article I&#8217;ve avoided using certain best practices (such as inserting Flash content using SWFObject &#8211; <a title="SWFObject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file" href="http://code.google.com/p/swfobject/" target="_blank">click here to find out more about the SWFObject project</a>). The reason being it helps concentrate the article on the core concepts we&#8217;re trying to get across and not get too distracted with best practices which you are probably already aware of (or should be).</p>
<p>But for those people who may be new to some of the &#8216;best practice&#8217; concepts I mention through the course of this article, for you I have referenced external material that will help you appreciate the benefits of the best practices I mention and encourage you to adhere to them.</p>
<p>Now back to the article.</p>
<p>As I said, if you need to access your Flash/<strong class="keyword">ActionScript</strong> functions via <strong class="keyword">JavaScript</strong> then the easiest way to do this is with the ExternalInterface API (<a title="Adobe ActionScript 3.0 Language and Components Referenc" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html" target="_blank">click here to read the Adobe documentation</a>).</p>
<p>The basic premise is to use the <code><a title="Registers an ActionScript method as callable from the container." href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#addCallback()" target="_blank">addCallback</a></code> method of the ExternalInterface API to register your Flash <strong class="keyword">ActionScript</strong> functions so they become accessible to <strong class="keyword">JavaScript</strong>.</p>
<p>An example of this is as follows&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * @param {String}, name that JavaScript uses to access ActionScript function
 * @param {Function}, the ActionScript function to be executed
 */</span>
ExternalInterface.<span style="color: #006600;">addCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myRegisteredFunctionName&quot;</span>, displayMessage<span style="color: #66cc66;">&#41;</span>;</pre></div></div>
<p>We&#8217;ll just imagine that you&#8217;ve written your <strong class="keyword">ActionScript</strong> function <code>displayMessage</code> already (as it doesn&#8217;t really matter for this article what the function does), but lets say it excepts one parameter, a string, which is then used to update a text field in the Flash movie.</p>
<p>This would look something like&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> displayMessage<span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// 'txtfield' is the instance name we have given the dynamic input field</span>
	txtfield.<span style="color: #0066CC;">text</span> = msg;
<span style="color: #66cc66;">&#125;</span></pre></div></div>
<p>Now the next part is to call the registered function via <strong class="keyword">JavaScript</strong>.</p>
<p>To do this from <strong class="keyword">JavaScript</strong> you need to get a reference to the Flash Player object, which can be a bit tricky as there are two basic versions that run in the browser: ActiveX and the plug-in version.</p>
<p>The ActiveX version runs natively in Internet Explorer, while the plug-in version is used by all other browsers.</p>
<p>To grab a reference to the ActiveX version you&#8217;ll need to assign the <code>&lt;object&gt;</code> tag an id attribute/value. I&#8217;ve used &#8220;InternetExplorerID&#8221; but you can call it whatever you want.</p>
<p>To grab a reference to the Embed version you&#8217;ll need to assign the <code>&lt;embed&gt;</code> tag a name attribute/value. I&#8217;ve used &#8220;AllOtherBrowsersID&#8221; but again you can call it whatever you want.</p>
<p>For example&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- notice I've stripped out a lot of code for the sake of brevity --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">object</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;InternetExplorerID&quot;</span>...&gt;</span>;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;TEST.swf&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;embed <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;AllOtherBrowsersID&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;TEST.swf&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>embed&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">object</span>&gt;</span></pre></div></div>
<p>Now we know what attributes to assign to each Flash Player version how do we actually access them from <strong class="keyword">JavaScript</strong>? Guess what, our old favourite&#8230; the cross-browser dilemma is back! Internet Explorer uses <code>window.<em>objectID</em></code> while all other browsers use <code>window.document.<em>embedName</em></code></p>
<p>Now i&#8217;ve seen some people suggest using browser-sniffing to achieve the result we&#8217;re after and although this article isn&#8217;t about best practices but trying to give you the core answers you&#8217;re looking for, I still can&#8217;t bring myself to use <strong class="keyword">JavaScript</strong> code like <code>if(navigator.appName.indexOf("Microsoft") != -1)</code> !</p>
<p>So we&#8217;ll try something a little different&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Internet Explorer</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">document</span>.<span style="color: #660066;">AllOtherBrowsersID</span> <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		flash <span style="color: #339933;">=</span> window.<span style="color: #660066;">InternetExplorerID</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		flash <span style="color: #339933;">=</span> window.<span style="color: #660066;">document</span>.<span style="color: #660066;">AllOtherBrowsersID</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	callFlashFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>
<p>The above code should require no extra explanation but just in case&#8230;</p>
<p>We wait until the page has finished loading and then we check whether the non-Internet Explorer way of accessing the Flash Player object is undefined (we could shrink this down further with a ternary operator but lets keep it simple for examples sake), if it is undefined then that means we&#8217;re using Internet Explorer and depending on the results we create a global variable to store the Flash Player version (<em>I know, I know, &#8220;global variables are evil&#8221; and best practice is to avoid cluttering the global name space - see: <a title="Global Domination" href="http://yuiblog.com/blog/2006/06/01/global-domination/" target="_blank">http://yuiblog.com/blog/2006/06/01/global-domination/</a> for more information)</em>.</p>
<p><code>callFlashFunction</code> does exactly what it says on the tin and calls the Flash registered function.</p>
<p>Now, <code>callFlashFunction</code> *<strong class="keyword">would</strong>* normally just use the global variable &#8220;flash&#8221; followed by a dot and the name of the registered Flash function, such as&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// &quot;flash&quot; is a reference to the global variable we created earlier</span>
flash.<span style="color: #660066;">registeredFunctionName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my message from JavaScript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>&#8230;but the problem we have now is that although the above line of code works fine with Internet Explorer (e.g. when the page loads the Flash function is called successfully) we have an issue with all other browsers that call the function too early (before the Flash movie has finished loading) and thus it appears as if nothing happens for those browsers.</p>
<p>So to work around this issue we must use a recursive function that checks the Flash registered function and if it returns <code>undefined</code> then recurse (i.e. the <strong class="keyword">JavaScript</strong> function calls itself) over and over until the Flash registered function is available to the browser.</p>
<p>The code to do this is as follows&#8230;</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> callFlashFunction <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>flash.<span style="color: #660066;">registeredFunctionName</span> <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		setTimeout<span style="color: #009900;">&#40;</span>callFlashFunction<span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		flash.<span style="color: #660066;">registeredFunctionName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my message from JavaScript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>
<p>And that is all there is to it, not to horrible I guess.</p>
<p>Any other perspectives? Feel free to let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.integralist.co.uk/actionscript/calling-an-actionscript-function-from-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
