| By Michael Girouard | Article Rating: |
|
| August 24, 2008 02:45 PM EDT | Reads: |
2,808 |
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,808
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.
- 4th International Cloud Computing Conference & Expo Starts Today
- Cloud Expo New York Call for Papers Deadline December 15
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- GovIT Expo Highlights Cloud Computing
- Google Wave
- Adaptivity & Cloud Computing: Exclusive Q&A with CEO Tony Bishop
- Cloud Computing Can Revitalize Your Career as Software Developer
- Oracle+MySQL Opponents Take to the Barricades
- Virtualization Expo Call for Papers Deadline December 15
- Vivek Kundra: "Engage the American People in their Daily Digital Lives"
- Instant Professionalism Online Despite Yourself...with Ulitzer
- Software Should Be Creative
- 4th International Cloud Computing Conference & Expo Starts Today
- Yahoo! Named “Platinum Sponsor” of Cloud Computing Expo
- Deputy CIO of the CIA to Keynote 1st Annual GovIT Expo
- Cloud Expo New York Call for Papers Deadline December 15
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- GovIT Expo Highlights Cloud Computing
- Google Wave
- Live Demo of Yahoo! Query Language to be Given at 4th Cloud Computing Expo
- Adaptivity & Cloud Computing: Exclusive Q&A with CEO Tony Bishop
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Goes After Gmail
- Where Are RIA Technologies Headed in 2008?
- Personal Branding Checklist
- The Top 250 Players in the Cloud Computing Ecosystem
- Wal-Mart To Sell $399 Ubuntu Linux-based Laptop with Google Operating System
- Google's OpenSocial: A Technical Overview and Critique
- Why Microsoft Loves Google's Android
- Web 2.0 Is Dead And It's Time To Get Out Of This Mess!
- i-Technology Blog: Google Trends on Java, McNealy, AJAX, and SOA Give Pause For Thought
- Android: Who Hates Google Over the Phone?
- Google Sales Surge 57% and Net Income 46% in Quarter 3
- i-Technology Blog: Is There Life Beyond Google?
- Social Network Wars: Google + Everyone Else vs Facebook































