cross browser getElement function
in the ever changing world of browser warfare it seems as if the may never be a completely universal way of developing web applications. i’m constantly having to find ways to carry out a particular task that will yield the same results on all browsers. one such task is getElementById. until we can safely say that every web browser out there will perform the same task, or even recognize is, we need to determine what action we really need to take.
i am not an advocate of writing blatant browser detection scripts and then writing subsequent code based on what browser is detected. its poor coding, and it can get extremely too messy very quickly. instead i prefer to write specific functions that attempt to perform the task at hand a few different ways until the right way is determined. it makes the flow of the overall program much more logical.
the function
function getElement(target) {
if( document.getElementById ) {
object = document.getElementById(target);
} else if( document.all ) {
object = document.all[target];
} else if( document.layers ) {
object = document.layers[target];
}
return object;
}
how to call
var object = getElement("myId");
the function simply checks which method works, and returns the object. simple. so no matter what browser is being used, a simple call to getElement will return the correct getElementById results.




