Recent posts

Recent comments

Archive

Calender

«   2024/12   »
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
incompleted - getElementsByClassName
 
if ( ! document.getElementsByClassName ){ 
    (function (){ 

        document.getElementsByClassName = function ( className ){ 
                // if ( this.querySelectorAll ){  return this.querySelectorAll( "." + className );  } 

                var children = this.getElementsByTagName( "*" ); 
                var object = { length: 0 }; 

                var count = 0, contain = false, x, n, child, classes; 

                for ( x = 0; x < children.length; x++ ){ 
                        child = children[ x ]; 
                        classes = child.className.split( / +/g ); 

                        for ( n = 0; n < classes.length; n++ ){   if ( classes[ n ] == className ) contain = true;  } 

                        if ( contain ){ object[ count ] = child; count++; } 

                        contain = false; 
                } 

                object.length = count; 
                return object; 
        }; 
 
        document.getElementsByClassName.toString = function (){ return "function getElementsByClassName() { [native code] }" }; 

        var currentWindow = document.defaultView ? document.defaultView : document.parentWindow; 

        if ( "Element" in currentWindow ){ 
            Element.prototype.getElementsByClassName = document.getElementsByClassName; 
        } 
        else { 
            var tableElements = [ "table", "thead", "tfoot", "tbody" ]; 

            var create = document.createElement; 
            document.createElement = function ( tagName ){ 
                var element = create( tagName ); 
                element.getElementsByClassName = document.getElementsByClassName; 

                var tagName = tagName.toLowerCase(); 
                if ( tagName == "tr" ) redefineInsertProperty( element, "insertCell" ); 
                for ( var x = 0; x < tableElements.length; x++ ){ 
                    if ( tagName == tableElements[ x ] ) redefineInsertProperty( element, "insertRow" ); 
                } 

                return element; 
            }; 
            document.createElement.toString = function (){ return "function createElement() { [native code] }" }; 

            function redefineInsertProperty ( elem, prop ){ 
                var insert = elem[ prop ]; 
                elem[ prop ] = function ( index ){ 
                        //var index = index || 0; 
                        var child = insert( index ); 
                        child.getElementsByClassName = document.getElementsByClassName; 
                        if ( child.tagName.toLowerCase() == "tr" ) redefineInsertProperty( child, "insertCell" ); 
                        return child; 
                }; 
                elem[ prop ].toString = function (){ return "function " + prop + "() { [native code] }" }; 
            } 

            if ( document.addEventListener ){ 
                document.addEventListener( "DOMContentLoaded", function (){ 
                        document.removeEventListener( "DOMContentLoaded", arguments.callee, false ); 
                        addProperty(); 
                }, false ); 
            } 
            else if ( document.attachEvent ){ 
                document.attachEvent( "onreadystatechange", function (){ 
                        if ( document.readyState === "complete" ){ 
                            document.detachEvent( "onreadystatechange", arguments.callee ); 
                            addProperty(); 
                        } 
                } ); 
            } 

            function addProperty (){ 
                var allTags = document.getElementsByTagName( "*" ); 
                var n, x, tag, tagName; 

                for ( n = 0; n < allTags.length; n++ ){ 
                        tag = allTags[ n ]; 
                        if ( tag.getElementsByClassName ) continue; 
                        tag.getElementsByClassName = document.getElementsByClassName; 

                        tagName = tag.tagName.toLowerCase(); 
                        if ( tagName == "tr" ) redefineInsertProperty( tag, "insertCell" ); 

                        for ( x = 0; x < tableElements.length; x++ ){ 
                            if ( tagName == tableElements[ x ] ) redefineInsertProperty( tag, "insertRow" ); 
                        } 
                } 
            } 

        } 
    }()); 
}