最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - [function].apply() causing "JScript object expected" error in IE - Stack Overflow

programmeradmin1浏览0评论

The following line of code causes a "JScript object expected" error in IE:

hideElements.apply(window, elements);

According to IE, the 'expected JScript object' refers to hideElements, which is a function that takes any number of HTML objects as arguments and hide them.

Concretely, I retrieve an array of HTML objects via a call to getElementsByTagName, and I would like to pass this array as a list of arguments to the function hideElements. The JS function apply() is exactly what I need in that case. I know I could surely write my code differently, but since this works perfectly on Firefox and Chrome, and is technically correct, I'd really like to know why IE gets stuck there.

I've determined that, when the line is executed:

  • window is not null and of type Window;
  • elements is not null and of type HTMLCollection; and
  • hideElements is a fully functional JS function (that works perfectly on its own and that is fully loaded when the code above is executed).

The following line of code causes a "JScript object expected" error in IE:

hideElements.apply(window, elements);

According to IE, the 'expected JScript object' refers to hideElements, which is a function that takes any number of HTML objects as arguments and hide them.

Concretely, I retrieve an array of HTML objects via a call to getElementsByTagName, and I would like to pass this array as a list of arguments to the function hideElements. The JS function apply() is exactly what I need in that case. I know I could surely write my code differently, but since this works perfectly on Firefox and Chrome, and is technically correct, I'd really like to know why IE gets stuck there.

I've determined that, when the line is executed:

  • window is not null and of type Window;
  • elements is not null and of type HTMLCollection; and
  • hideElements is a fully functional JS function (that works perfectly on its own and that is fully loaded when the code above is executed).
Share asked Jun 20, 2012 at 4:46 AxelAxel 9661 gold badge8 silver badges25 bronze badges 6
  • I don't think IE likes window to be treated like an ordinary JS object, but I could be wrong. – Pointy Commented Jun 20, 2012 at 4:49
  • 1 I'm not sure an HTMLCollection is patible with an array (it's live), have you tried making a copy into an actual [] array first ? – Eric Commented Jun 20, 2012 at 5:00
  • Thanks Eric, that's where the problem es from. Unfortunately, converting the collection to an array is not worse doing in my case. I guess my only choice is to use another approach. Makes you wonder why this works fine with FF and Chrome, though... Don't hesitate to post the answer and I'll accept it. – Axel Commented Jun 20, 2012 at 5:21
  • You could also use call instead of apply and then have your hideElements function handle the elements collection. "Calls a function with a given this value and arguments provided individually." – Stefan Commented Jun 20, 2012 at 6:14
  • I simply changed the code to hideElements(elements), and modified the function hideElements so that if the first argument is an array, it loops through it; otherwise it loops through the arguments. – Axel Commented Jun 20, 2012 at 6:19
 |  Show 1 more ment

1 Answer 1

Reset to default 7

getElementsByTagName returns a nodeList. apply expects the second argument to be an array. If you convert elements to a real array (using a loop), it should work.

Note: In IE<9, it's not possible to use Array.prototype.slice.call(elements) for that, so a loop is the safest way to create an array, like:

function nodeList2Array(nodes){
  var arr = [];
  for (var i=1; i<nodes.length;(i+=1)){
    arr.push(nodes[i]);
  }
  return arr;
}

now: hideElements.apply(window, nodeList2Array(elements)); should work.

发布评论

评论列表(0)

  1. 暂无评论