My comments on ...

... everything bothering me

The "classic example"

, ,

Inspired by hallvors' Capability Detection and a follow-up post from Crisp, I tried to improve on the "classic example"...

if (!document.getElementById)
{
   (function()
    {
     var obj = {};
     if (document.all)
     {
       obj = document.all;
     }
     else if (document.layers)
     {
       obj = document.layers;
     }
     document.getElementById = function(id){return obj[id]};
    }
   )();
}

edit: I also thought about really fixing this, as to change the output from "undefined" to "null" in the case that there is no element with that Id (according to spec), but I figured if someone is stupid enough to check explicitly against null or undefined, instead of normal object detection, then he earned the burn. I really can't imagine a scenario where this "feature" of the function is mandatory.

If there ever occurred such a scenario, however... replace

document.getElementById = function(id){return obj[id]};

by

document.getElementById = function(id){return obj[id] ? obj[id] : null};

or, if there are browsers out there that return "undefined" instead of "null" (natively), you might want to add this instead (update 2007/08/28: Don't use, it's dirty)

if (!document.getElementById(null) && document.getElementById(null) !== null)
{
   (function()
    {
     var obj = document;
     var func = 'getElementById';
     var key = '_';
     while (obj[key+func]) {key+=key};
     obj[key+func] = obj[func];
     obj[func] = function(id){return obj[key+func](id) || null}
    }
   )();
}

The better way of Capability Detection, Part 2The "classic example", take 2

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

February 2012
M T W T F S S
January 2012March 2012
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29