JSUtility_JS

      1| function getElementsByClass(classname)
      2| {
      3|     if(document.getElementsByClassName)
      4|     {
      5|         /*use native implementation if browser is new enough like CMS over at (http://stackoverflow.com/questions/1933602/how-to-getelementbyclass-instead-of-getelementbyid-with-javscript)recommends in his answer.*/
      6| 
      7|         return document.getElementsByClassName(classname);
      8|     }
      9|     else
    10|     {
    11|         /*
    12|         Rolled my own similar to what is recommended in JavaScript & DHTML Cookbook (2nd edition) 14.14 pp.480-1 (http://www.amazon.com/gp/product/0596514085?ie=UTF8&tag=keithgallcom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596514085).
    13|         */
    14|         var allElements = (document.all) ? document.all : document.getElementsByTagName("*");
    15|         var ELen = allElements.length;
    16| 
    17|         var results = new Array();
    18|         var regEx = new RegExp("\\b" + classname + "\\b");
    19| 
    20|         for(var i = 0; i < ELen; i++)
    21|         {
    22|             //The '.className' is not the same as element 'classname'. NEVER CONFUSE!
    23|             if(regEx.test(allElements[i].className))
    24|             {
    25|                 results.push(allElements[i]);
    26|             }
    27|         }
    28| 
    29|         return results;
    30|     }
    31| }
    32| 
    33| function getExternalStyle(element, style)
    34| {
    35|     /* Because the 'element.style' method only works for inline styles, this function must be used to get the
    36|     external style.*/
    37|     //Based on a tip from http://www.quirksmode.org/dom/getstyles.html
    38|     if(window.getComputedStyle)
    39|     {
    40|         /*Get style from Gecko-based (Firefox) DOM, WebKit-based (Chrome/Safari) DOM, and Opera-based DOM.
    41|         '.getPropertyValue()' required for WebKit-based (Chrome/Safari) DOM as WebKit-based (Chrome/Safari) DOM
    42|         returns an index value instead of a CSS property.*/
    43|         var finalval = window.getComputedStyle(element, null).getPropertyValue(style);
    44|     }
    45|     else
    46|     {
    47|         //Get style from IE-based DOM.
    48|         var finalval = element.currentStyle[style];
    49|     }
    50| 
    51|     return finalval;
    52| }
    53| 
    54| function setStyleHack(element, styleData)
    55| {
    56|     /*According to http://www.quirksmode.org/dom/w3c_core.html, IE5-IE7 doesn't implement 'style' in the
    57|     '.setAttribute'. This hack gets around that for IE7, the version that shipped with Vista, so that I can
    58|     implement style creation on a node for the Vista Death Clock.*/
    59|     //Based on a tip from http://webbugtrack.blogspot.com/2007/10/bug-245-setattribute-style-does-not.html
    60|     var userAgent = navigator.userAgent;
    61| 
    62|     var MSIEOffset = userAgent.indexOf("MSIE ");
    63|     var startIE = MSIEOffset + 5;
    64|     var endIE = userAgent.indexOf(";", MSIEOffset);
    65|     var subNumIE = userAgent.substring(startIE, endIE);
    66|     var pIEInt = parseInt(subNumIE);
    67| 
    68|     var TridentOffset = userAgent.indexOf("Trident/");
    69|     var pTrInt = 0;
    70| 
    71|     if(TridentOffset != -1)
    72|     {
    73|         var startTr = TridentOffset + 8;
    74|         var endTr = userAgent.indexOf(")", TridentOffset);
    75| 
    76|         var subNumTr = userAgent.substring(startTr, endTr);
    77| 
    78|         pTrInt = parseInt(subNumTr);
    79|     }
    80|     else
    81|     {
    82|         pTrInt = 0;
    83|     }
    84| 
    85|     if(pIEInt > 7 || MSIEOffset == -1)
    86|     {
    87|         //The W3C-based DOM Method for all modern browsers including IE8+.
    88|         element.setAttribute('style', styleData);
    89|     }
    90|     else
    91|     {
    92|         if(pTrInt != 0)
    93|         {
    94|             //The W3C-based DOM Method for all modern browsers including IE8+.
    95|             element.setAttribute('style', styleData);
    96|         }
    97|         else
    98|         {
    99|             //The .cssText hack for IE7 and below.
  100|             element.style.setAttribute('cssText', styleData);
  101|         }
  102|     }
  103| 
  104| }