V
V
vohaha2014-11-19 06:23:31
JavaScript
vohaha, 2014-11-19 06:23:31

How to solve .length issue after getElementsByClassName hack in ie?

There is a parallax.js library which has getElementsByClassName. To work in IE7> registered:

function getElementsByClassName(node, classname) {
  var a = [];
  var re = new RegExp('(^| )'+classname+'( |$)');
  var els = node.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
  return a;
}
var parallaxLayer = getElementsByClassName(document.body,'layer');

But now the error in all browsers is: " Cannot read property 'length' of undefined " (chrome, opera), " Unable to get property 'length' of undefined or null reference " (IE), " this.layers is undefined " (FF ).
Is it possible to solve this problem?
Here is a small code example (comments where the getElementsByClassName method is replaced and where it throws an error with length)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2014-11-19
@mlnkv

I wonder why you need support for IE 7?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question