Technology Programming

JavaScript By Example

There are a number of places within your JavaScript code where you need to be able to run something different depending on which browser it is running in. What we need to do in these instances is to test which way the particular browser supports and process the code that way. To do this we use what is known as feature sensing to test which way the particular browser expects us to use by testing what the browser supports.

There are at least three different approaches that we can take to performing these tests. The simplest to implement but the least efficient is to perform the test each and every time that we need to perform the processing. This is the least efficient because we know from the first time that we run the test which way is supported and yet we rerun the same test each subsequent time we need to use the code. We can improve on the efficiency by placing the one feature sensing if statement in our initial code which creates a function or method that we can run which contains the specific code required by the particular browser where the code creates one out of two or more different versions of the function/method so that when that function or method is called it will run the version of the code that the browser will support. This is far more efficient because the test for which version of the code to use runs only once.

The third approach is to use lazy definition. This goes one step further to make the code even more efficient in that when you use lazy definition the test for which version of the function to create is run the first time it is needed rather than at the very start and so doesn't get run at all if there is nothing in the code that needs to call it.

In this example we have converted the addEvent code that handles both modern browsers and old versions of Internet Explorer so that the if statement determining which version of the addEvent function to use gets run the first time the addEvent function is called rather than when the code is first loaded.

addEvent = function(ob, type, fn) {
if (window.addEventListener)
  addEvent = function(ob, type, fn ) {
    ob.addEventListener(type, fn, false );
  };
  else if (document.attachEvent)
    addEvent = function(ob, type, fn ) {
      var eProp = type + fn;
      ob['e'+eProp] = fn;
      ob[eProp] = function(){ob['e'+eProp]( window.event );};
      ob.attachEvent( 'on'+type, o[eProp]);
    };
  else return;
addEvent(ob, type, fn):
};

 
addEvent(document.getElementById('myid'),
  'click',
  myfunction);

SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*