Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object doesn't support property or method 'getElementsByClassName'? #111

Closed
only1chip opened this issue Feb 6, 2013 · 13 comments
Closed

Comments

@only1chip
Copy link

First off: love the script - thanks for writing it.

It's probably something stupid that I'm doing, but I'm getting this error when testing in IE pre version 9:

SCRIPT438: Object doesn't support property or method 'getElementsByClassName'
list.js, line 615 character 21

I tried adding a couple different 'getElementsByClassName' functions for older browser versions, but none have worked. I doubt this is a bug of List.js - but I am stumped - any help would be appreciated.

My page is here: www.sureservo.com/gearbox/selector/

@javve
Copy link
Owner

javve commented Feb 8, 2013

Thanks for your compliments!

Hm, very weird. Seems like document.getElementsByClassName at line 613 returns a function in IE8 at your website, but not at other sites. Maybe I have to update the check. Until I've fixed it you can try update the code to document.body.getElementsByClassName. Ping me about the results.

@javve javve closed this as completed Feb 8, 2013
@only1chip
Copy link
Author

Jonny,

Thanks for looking at this - and sorry for my delayed reply...

I just tried your suggestion on my testing server (for me, the 'if'
statement was on line 612), but it doesn't seem to work either...

I get the following errors in the Chrome console:

SCRIPT5007: Unable to get the value of the property
"getElementByClassName': object is null or undefined
list.js, line 612 character 9

SCRIPT5009: List.js is undefined
gearbox-selector.js, line 258, character 1

It seems to break the page for other browsers/version also - or else I
would publish it so you could see...

I've posted a banner for IE8 users indicating that I don't support them - &
I can live with that... If you have any other ideas - I'm happy to try...
but it's not a show stopper... and we do really like the List.js...

Chip

On Fri, Feb 8, 2013 at 5:20 PM, Jonny Strömberg notifications@github.comwrote:

Thanks for your compliments!

Hm, very weird. Seems like document.getElementsByClassName at line 613
returns a function in IE8 at your website, but not at other sites. Maybe I
have to update the check. Until I've fixed it you can try update the code
to document.body.getElementsByClassName. Ping me about the results.


Reply to this email directly or view it on GitHubhttps://github.com//issues/111#issuecomment-13315700..

@javve
Copy link
Owner

javve commented Feb 15, 2013

@only1chip Seems like you forgot a s in getElementsByClassName :) Try again and tell me how it went!

@only1chip
Copy link
Author

Oops - that was only a typo in my email to you - working at two stations...
had to transpose from one to the other...

All I was really adding was the ".body" - so I didn't touch the rest of the
phrase...

Since it seems you are online - I'm going to publish the change for a few
minutes - maybe you can look??? I can't leave it for long though...

Chip

On Fri, Feb 15, 2013 at 5:30 PM, Jonny Strömberg
notifications@github.comwrote:

@only1chip https://github.com/only1chip Seems like you forgot a s in
getElementsByClassName :) Try again and tell me how it went!


Reply to this email directly or view it on GitHubhttps://github.com//issues/111#issuecomment-13631677.

@javve
Copy link
Owner

javve commented Feb 15, 2013

Hmm, well. Then I have another suggestion. Update the getByClass to look like this: http://jsfiddle.net/cLHuP/

   getByClass: function(searchClass,node,single) {
        if (node.getElementsByClassName) {
            if (single) {
                return node.getElementsByClassName(searchClass)[0];
            } else {
                return node.getElementsByClassName(searchClass);
            }
        } else {
            var classElements = [],
                tag = '*';
            if (node == null) {
                node = document;
            }
            var els = node.getElementsByTagName(tag);
            var elsLen = els.length;
            var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
            for (var i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                    if (single) {
                        return els[i];
                    } else {
                        classElements[j] = els[i];
                        j++;
                    }
                }
            }
            return classElements;
        }
   }

@only1chip
Copy link
Author

OK, I'm trying to do that but I keep getting syntax errors... certainly
my ignorance is the cause...

Should I be replacing all the functions inside the 'h = {'

or just the getByClass???

Chip

On Fri, Feb 15, 2013 at 5:44 PM, Jonny Strömberg
notifications@github.comwrote:

Hmm, well. Then I have another suggestion. Update the getByClass to look
like this: http://jsfiddle.net/cLHuP/

getByClass: function(searchClass,node,single) {
if (node.getElementsByClassName) {
if (single) {
return node.getElementsByClassName(searchClass)[0];
} else {
return node.getElementsByClassName(searchClass);
}
} else {
var classElements = [],
tag = '*';
if (node == null) {
node = document;
}
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
for (var i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
if (single) {
return els[i];
} else {
classElements[j] = els[i];
j++;
}
}
}
return classElements;
}
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/111#issuecomment-13632224.

@javve
Copy link
Owner

javve commented Feb 15, 2013

Just getByClass.
I've tested it locally in IE8 with both XP and Win7 and it works.

@only1chip
Copy link
Author

I certainly don't doubt that - I just can't figure out exactly which code
to replace and which ({({{({({({}}}}}({({{({(;;;;{{ to leave in...

feeling pretty stupid here...

Chip

On Fri, Feb 15, 2013 at 6:14 PM, Jonny Strömberg
notifications@github.comwrote:

Just getByClass.
I've tested it locally in IE8 with both XP and Win7 and it works.


Reply to this email directly or view it on GitHubhttps://github.com//issues/111#issuecomment-13633245.

@javve
Copy link
Owner

javve commented Feb 15, 2013

Hehe, well, remove everything from line 600 to the end with this.

/*
* These helper functions are not written by List.js author Jonny (they may have been
* adjusted, thought).
*/
h = {
    /*
    * Cross browser getElementsByClassName, which uses native
    * if it exists. Modified version of Dustin Diaz function:
    * http://www.dustindiaz.com/getelementsbyclass
    */
    getByClass: function(searchClass,node,single) {
        if (node.getElementsByClassName) {
            if (single) {
                return node.getElementsByClassName(searchClass)[0];
            } else {
                return node.getElementsByClassName(searchClass);
            }
        } else {
            var classElements = [],
                tag = '*';
            if (node == null) {
                node = document;
            }
            var els = node.getElementsByTagName(tag);
            var elsLen = els.length;
            var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
            for (var i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                    if (single) {
                        return els[i];
                    } else {
                        classElements[j] = els[i];
                        j++;
                    }
                }
            }
            return classElements;
        }
   },
    /* (elm, 'event' callback) Source: http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/ */
    addEvent: (function( window, document ) {
        if ( document.addEventListener ) {
            return function( elem, type, cb ) {
                if ((elem && !(elem instanceof Array) && !elem.length && !h.isNodeList(elem) && (elem.length !== 0)) || elem === window ) {
                    elem.addEventListener(type, cb, false );
                } else if ( elem && elem[0] !== undefined ) {
                    var len = elem.length;
                    for ( var i = 0; i < len; i++ ) {
                        h.addEvent(elem[i], type, cb);
                    }
                }
            };
        }
        else if ( document.attachEvent ) {
            return function ( elem, type, cb ) {
                if ((elem && !(elem instanceof Array) && !elem.length && !h.isNodeList(elem) && (elem.length !== 0)) || elem === window ) {
                    elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event); } );
                } else if ( elem && elem[0] !== undefined ) {
                    var len = elem.length;
                    for ( var i = 0; i < len; i++ ) {
                        h.addEvent( elem[i], type, cb );
                    }
                }
            };
        }
    })(this, document),
    /* (elm, attribute) Source: http://stackoverflow.com/questions/3755227/cross-browser-javascript-getattribute-method */
    getAttribute: function(ele, attr) {
        var result = (ele.getAttribute && ele.getAttribute(attr)) || null;
        if( !result ) {
            var attrs = ele.attributes;
            var length = attrs.length;
            for(var i = 0; i < length; i++) {
                if (attr[i] !== undefined) {
                    if(attr[i].nodeName === attr) {
                        result = attr[i].nodeValue;
                    }
                }
            }
        }
        return result;
    },
    /* http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript */
    isNodeList: function(nodes) {
        var result = Object.prototype.toString.call(nodes);
        if (typeof nodes === 'object' && /^\[object (HTMLCollection|NodeList|Object)\]$/.test(result) && (nodes.length == 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0))) {
            return true;
        }
        return false;
    },
    hasClass: function(ele, classN) {
        var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
        return (classes.search(classN) > -1);
    },
    addClass: function(ele, classN) {
        if (!this.hasClass(ele, classN)) {
            var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
            classes = classes + ' ' + classN + ' ';
            classes = classes.replace(/\s{2,}/g, ' ');
            ele.setAttribute('class', classes);
        }
    },
    removeClass: function(ele, classN) {
        if (this.hasClass(ele, classN)) {
            var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
            classes = classes.replace(classN, '');
            ele.setAttribute('class', classes);
        }
    },
    /*
    * The sort function. From http://my.opera.com/GreyWyvern/blog/show.dml/1671288
    */
    sorter: {
        alphanum: function(a,b,asc) {
            if (a === undefined || a === null) {
                a = "";
            }
            if (b === undefined || b === null) {
                b = "";
            }
            a = a.toString().replace(/&(lt|gt);/g, function (strMatch, p1){
                return (p1 == "lt")? "<" : ">";
            });
            a = a.replace(/<\/?[^>]+(>|$)/g, "");

            b = b.toString().replace(/&(lt|gt);/g, function (strMatch, p1){
                return (p1 == "lt")? "<" : ">";
            });
            b = b.replace(/<\/?[^>]+(>|$)/g, "");
            var aa = this.chunkify(a);
            var bb = this.chunkify(b);

            for (var x = 0; aa[x] && bb[x]; x++) {
                if (aa[x] !== bb[x]) {
                    var c = Number(aa[x]), d = Number(bb[x]);
                    if (asc) {
                        if (c == aa[x] && d == bb[x]) {
                            return c - d;
                        } else {
                            return (aa[x] > bb[x]) ? 1 : -1;
                        }
                    } else {
                        if (c == aa[x] && d == bb[x]) {
                            return d-c;//c - d;
                        } else {
                            return (aa[x] > bb[x]) ? -1 : 1; //(aa[x] > bb[x]) ? 1 : -1;
                        }
                    }
                }
            }
            return aa.length - bb.length;
        },
        chunkify: function(t) {
            var tz = [], x = 0, y = -1, n = 0, i, j;

            while (i = (j = t.charAt(x++)).charCodeAt(0)) {
                var m = (i == 45 || i == 46 || (i >=48 && i <= 57));
                if (m !== n) {
                    tz[++y] = "";
                    n = m;
                }
                tz[y] += j;
            }
            return tz;
        }
    }
};

window.List = List;
window.ListJsHelpers = h;
})(window);

@only1chip
Copy link
Author

OK, thanks!

No syntax errors, but no joy either...

You should be able to see the live site...

(note: I'm seeing "strike through' lines of code below (towards the end)

I really appreciate your time on this...

Chip

On Fri, Feb 15, 2013 at 6:24 PM, Jonny Strömberg
notifications@github.comwrote:

Hehe, well, remove everything from line 600 to the end with this.

/** These helper functions are not written by List.js author Jonny (they may have been* adjusted, thought)./h = {
/
* Cross browser getElementsByClassName, which uses native * if it exists. Modified version of Dustin Diaz function: * http://www.dustindiaz.com/getelementsbyclass
/
getByClass: function(searchClass,node,single) {
if (node.getElementsByClassName) {
if (single) {
return node.getElementsByClassName(searchClass)[0];
} else {
return node.getElementsByClassName(searchClass);
}
} else {
var classElements = [],
tag = '
';
if (node == null) {
node = document;
}
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
for (var i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
if (single) {
return els[i];
} else {
classElements[j] = els[i];
j++;
}
}
}
return classElements;
}

},
/* (elm, 'event' callback) Source: http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/ /
addEvent: (function( window, document ) {
if ( document.addEventListener ) {
return function( elem, type, cb ) {
if ((elem && !(elem instanceof Array) && !elem.length && !h.isNodeList(elem) && (elem.length !== 0)) || elem === window *
) {
elem.addEventListener(type, cb, false );
} else if ( elem && elem[0] !== undefined ) {
var len = elem.length;
for ( var i = 0; i < len; i++ ) {
h.addEvent(elem[i], type, cb);
}
}
};
}
else if ( document.attachEvent ) {
return function ( elem, type, cb ) {
if ((elem && !(elem instanceof Array) && !elem.length && !h.isNodeList(elem) && (elem.length !== 0)) || elem === window *) {
elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event); } );
} else if ( elem && elem[0] !== undefined ) {
var len = elem.length;
for ( var i = 0; i < len; i++ ) {
h.addEvent( elem[i], type, cb );
}
}
};
}
})(this, document),
/
(elm, attribute) Source: http://stackoverflow.com/questions/3755227/cross-browser-javascript-getattribute-method /
getAttribute: function(ele, attr) {
var result = (ele.getAttribute && ele.getAttribute(attr)) || null;
if( !result ) {
var attrs = ele.attributes;
var length = attrs.length;
for(var i = 0; i < length; i++) {
if (attr[i] !== undefined) {
if(attr[i].nodeName === attr) {
result = attr[i].nodeValue;
}
}
}
}
return result;
},
/
http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript /
isNodeList: function(nodes) {
var result = Object.prototype.toString.call(nodes);
if (typeof nodes === 'object' && /^[object (HTMLCollection|NodeList|Object)]$/.test(result) && (nodes.length == 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0))) {
return true;
}
return false;
},
hasClass: function(ele, classN) {
var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
return (classes.search(classN) > -1);
},
addClass: function(ele, classN) {
if (!this.hasClass(ele, classN)) {
var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
classes = classes + ' ' + classN + ' ';
classes = classes.replace(/\s{2,}/g, ' ');
ele.setAttribute('class', classes);
}
},
removeClass: function(ele, classN) {
if (this.hasClass(ele, classN)) {
var classes = this.getAttribute(ele, 'class') || this.getAttribute(ele, 'className') || "";
classes = classes.replace(classN, '');
ele.setAttribute('class', classes);
}
},
/
* The sort function. From http://my.opera.com/GreyWyvern/blog/show.dml/1671288 */
sorter: {
alphanum: function(a,b,asc) {
if (a === undefined || a === null) {
a = "";
}
if (b === undefined || b === null) {
b = "";
}
a = a.toString().replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
a = a.replace(/</?[^>]+(>|$)/g, "");

        b = b.toString().replace(/&(lt|gt);/g, function (strMatch, p1){
            return (p1 == "lt")? "<" : ">";
        });
        b = b.replace(/<\/?[^>]+(>|$)/g, "");
        var aa = this.chunkify(a);
        var bb = this.chunkify(b);

        for (var x = 0; aa[x] && bb[x]; x++) {
            if (aa[x] !== bb[x]) {
                var c = Number(aa[x]), d = Number(bb[x]);
                if (asc) {
                    if (c == aa[x] && d == bb[x]) {
                        return c - d;
                    } else {
                        return (aa[x] > bb[x]) ? 1 : -1;
                    }
                } else {
                    if (c == aa[x] && d == bb[x]) {
                        return d-c;//c - d;
                    } else {
                        return (aa[x] > bb[x]) ? -1 : 1; //(aa[x] > bb[x]) ? 1 : -1;
                    }
                }
            }
        }
        return aa.length - bb.length;
    },
    chunkify: function(t) {
        var tz = [], x = 0, y = -1, n = 0, i, j;

        while (i = (j = t.charAt(x++)).charCodeAt(0)) {
            var m = (i == 45 || i == 46 || (i >=48 && i <= 57));
            if (m !== n) {
                tz[++y] = "";
                n = m;
            }
            tz[y] += j;
        }
        return tz;
    }
}};

window.List = List;window.ListJsHelpers = h;})(window);


Reply to this email directly or view it on GitHubhttps://github.com//issues/111#issuecomment-13633535.

@only1chip
Copy link
Author

OK - I'm leaving those changes in place. They don't seem to break the modern browsers/versions - but I still don't see it working in IE8... perhaps we can try again next week???

Wife is annoyed - almost 7pm here on Friday night... must go fix that bug...

Thanks again! If you ever need any automation products - e-mail me...

Chip

@javve
Copy link
Owner

javve commented Feb 16, 2013

Hmm, I'll take a look some other day! (its 01:00 am between Friday and Saturday here ;))

Automation products, I'll keep that in mind! 🍺

@masterkain
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants