| By Michael Girouard | Article Rating: |
|
| August 24, 2008 02:45 PM EDT | Reads: |
2,753 |
Mike Girouard's Blog
Load time configuration is the process where a JavaScript application configures itself as it is being loaded. This pattern is most commonly found in libraries in which they configure themselves at load time to be optimized for a particular browser.
Load time configuration is also known as load time branching.
Motivation
The primary motivation behind load time configuration is to optimize conditional operations such as generating XMLHttpRequest instances or adding event listeners. Since both operations vary amongst clients, specific conditions must be checked before proceeding. The load time configuration pattern runs once at load time and sets the resulting value so that it doesn’t need to be checked again.
Implementation
The most common technique for implementing load time configuration is by way of a Self-invoking function which returns the correctly configured value.
The example below demonstrates how load time configuration can be used to normalize getting an XMLHttpRequest object.
var getXHR = (function () {
if (window.XMLHttpRequest) {
return function () {
return new XMLHttpRequest;
}
}
else if (window.ActiveXObject) {
return function () {
/* Msxml2 and Msxml3 checkes have been omitted
* for simplicity's sake */
return new ActiveXObject('Microsoft.XMLHTTP');
}
}
})();
First, a getXHR variable is declared and it’s value is being assigned by the self-invoking function that follows. Upon execution, a conditional is run which determines the correct method of obtaining an XMLHttpRequest object. When a match is found, a wrapper function is returned and stored as the value for getXHR variable.
Conclusion
The load time conditional pattern is used to configure a conditional value at load time. The result is a fully optimized value, specific to the the environment in which it is to be used.
Published August 24, 2008 Reads 2,753
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Michael Girouard
Mike Girouard is a front-end web developer living in New York City. As the Sr. Developer at the creative agency Magnani Caruso Dutton, he takes pride in his ability to introduce web standards and beautiful code to industry giants such as Discover and AT&T. In his offtime, Girouard goes right back to his editor and codes toward his latest open-source baby, Panda PHP Components. You can read more about him and his other projects on his blog, http://www.lovemikeg.com/blog.
- Open Source Java Guru Moving to Joost
- GITEX TECHNOLOGY WEEK 2009 Exhibitor Profiles
- Ulitzer Live! New Media Power Panel at Santa Clara Convention Center
- Turn Your Podcast Into an Interactive Live Streaming Experience
- New Virus Targets Facebook, MySpace and Twitter Users
- LA Phil Launches Microsite Celebrating Incoming Music Director
- Google Sites Exceed 10 Billion Video Views in August
- Xerox To Buy ACS for $6.4 Billion
- Warner Music Group Appoints Renowned Producer Rob Cavallo as Chief Creative Officer
- Mobile App or Mobile Web
- ITRI's FleXpeaker, an Innovative, Paper-Thin Speaker Technology, Receives 2009 Wall Street Journal Technology Innovation Award
- Apple Store Upper West Side Opening on Saturday, November 14
- Open Source Java Guru Moving to Joost
- GITEX TECHNOLOGY WEEK 2009 Exhibitor Profiles
- Apple Introduces New iPod nano With Built-in Video Camera
- MTV Video Music Award-Winning Green Day To Host Special Music Countdown on SIRIUS XM Radio
- Stewart McKie Launches Mobile Tagging and Content Delivery Topic on Ulitzer
- Technology Face-Off: Augmented Reality vs Mobile Image
- Apple Approves First Official Porn Star App for iPhone
- Pixamba CEO Launches Stock Photography Topic on Ulitzer
- Ulitzer Live! New Media Power Panel at Santa Clara Convention Center
- Turn Your Podcast Into an Interactive Live Streaming Experience
- New Virus Targets Facebook, MySpace and Twitter Users
- Sybase and Verizon Team up to Manage Mobility Solutions For Enterprises Worldwide
- Video Conference with Flex & FMS
- The Top 250 Players in the Cloud Computing Ecosystem
- AJAX and Enterprise RIA Tools - JSF, Flex, and JavaFX
- SYS-CON Events Announces iTVcon Internet TV Conference & Expo 2007
- iTVCon - Internet Video Conference & Expo Registrations Now Open
- Internet Video Update: First "Webisode" of Quarterlife Will Air on MySpaceTV
- Microsoft's Flash-Killer Silverlight Streaming Video Plug-in Released
- "TV Anywhere, Anytime" Gets a Boost...From Joost
- Android: Who Hates Google Over the Phone?
- From Enterprise to Cloud, Virtualization Today on SYS-CON.TV
- iTVCon - Starts Next Monday! Check Out the Full Speaker Lineup
- Virtualization: "The Leading Technology of the 21st Century"


























