Skip navigation.

My comments on ...

... everything bothering me

Posts tagged with "Web Developement"

The "classic example", take 2

, , ,

if(!document.getElementById)void()
else
{
   //continue
}

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 2

, ,

I haven't found time earlier to write this down, so I've made a note with the code attached and do it now :wink:

In execution of my occupation, last week (or so) I wrote a post "nagging at the nagger". This is round 2, he has given response (he basically says it's not worth the hassle).

I've removed the "finally" part and shortened the most important part down from two lines to one (thereby obscuring it a bit, *sigh*).

Based on a suggestion from Crisp, I wrapped it around an anonymous function, to prevent the variables from being declared in global scope. I've put the function into the condition (as opposed to the other way round), because the program then can tell right away that this function does not need to be executed, as opposed to executing the function and then evaluating the condition.

if (!window.XMLHttpRequest)
{
    (function()
     {
      var temp;
      var types = [
          'MSXML2.XMLHTTP.6.0',
          'MSXML2.XMLHTTP.3.0'
      ];

      for (var i = 0; i < types.length; i++)
      {
          try
          {
              void((temp = function(){return new ActiveXObject(types[i])})());
          }
          catch {temp = null}
          if(temp) break;
      }
      window.XMLHttpRequest = temp || function(){return undefined};
     }
    )();
}


Now, let's have a look at the most tricky part:

void(temp = function(){return new ActiveXObject(types[i])}());

// reformatted looks like this:

void(                                            /* void the result, to protect browsers
                                                    that return/throw "false" or similar */
     (temp = function()                          /* assign a function to the variable... */
            {
               return new ActiveXObject(types[i])
            })()                                 /* and then call it by inserting parenthe-
                                                  ses after those enclosing the function */
     );


Interestingly, I haven't yet tested it, though. Funny thing, that.

The better way of Capability Detection

, ,

In response to Crisp's post about "capability detection" (and his comment system, that ate most of my messages...):

The goal was to optimize the code provided so the "if" statements were not part of the function anymore.

The code wrapped around the "finally" construct might stand alone, and I'm sure the code could be otherwise shortened, but I think this should work all in all...

if (!window.XMLHttpRequest)
{
     var temp;
     var types = [
         'MSXML2.XMLHTTP.6.0',
         'MSXML2.XMLHTTP.3.0'
     ];
 
     for (var i = 0; i < types.length; i++)
     {
         try
         {
             temp = function(){return new ActiveXObject(types[i])};
             void(temp());
         }
         catch(e) {temp = null;}
         finally {if(temp) break;}
     }
     window.XMLHttpRequest = temp || function(){return undefined};
}
December 2008
M T W T F S S
November 2008January 2009
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 30 31