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.
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