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 typeWindow
;elements
is not null and of typeHTMLCollection
; andhideElements
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 typeWindow
;elements
is not null and of typeHTMLCollection
; andhideElements
is a fully functional JS function (that works perfectly on its own and that is fully loaded when the code above is executed).
-
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 ofapply
and then have yourhideElements
function handle theelements
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 functionhideElements
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
1 Answer
Reset to default 7getElementsByTagName
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.