Forum Moderators: open

Message Too Old, No Replies

splice() problem

         

Scally_Ally

11:29 am on May 1, 2007 (gmt 0)

10+ Year Member



hi,
was just wondering if there are any inherent differences in the way browsers handle the splice() function?
I have written a script that stores the values of elements in a menu in an array, then to delete these elements from my array i use splice to easily delete taht element and any others that come after it.

It works fine in FF but not in others. Here is a sample of my code where the prob is occuring


if(s.display=='none') {
for(i=parseInt(idLevel); i<opened.length; i++){//close open ones higher than selected uip
document.getElementById(opened[i]).style.display = 'none';
}
//splice the array anmd then display correcr
opened.splice(parseInt(idLevel));
s.display = 'block';
opened[opened.length] = id;
} else {
for(i=parseInt(idLevel); i<opened.length; i++){//close open ones higher than selected uip
document.getElementById(opened[i]).style.display = 'none';
}
s.display = 'none';
opened.splice(parseInt(idLevel));
}

Any help would be appreciated,
Cheers
Ally

Scally_Ally

1:07 pm on May 1, 2007 (gmt 0)

10+ Year Member



Just to answer my own question, there is a compatibility prob in both IE and Opera..
what i had to do was use array.pop() and loop through how many times i wanted to do this and not just splice() a big chunk of the array off.

Ally

Dabrowski

3:49 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, there is not a compatibility problem in IE and Opera, you're just not using the function correctly.

See:
[w3schools.com...]

You need to specify 2 parameters, the first being the index, the second being how many elements to remove. You can also specify replacement elements, hence the definition of the word splice.

Hope this solves it.

penders

9:41 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, there is not a compatibility problem in IE and Opera, ...

w3schools does state the 2nd argument as being reqd and for browser compatibility I think it is. However, I have read several sources that state the 2nd arg as optional...

JavaScript: The Definitive Guide, 4th Edition By David Flanagan:

If this second argument is omitted, all array elements from the start element to the end of the array are removed.

And this is how it works in FF1.5, but not in IE6 or Op8 (both seem to default the 2nd arg to 0 [zero]). I suppose this could be a browser compatibility problem.

<edit>

Just to add, IE 5.01 does not support

splice()
at all, in case you still need to support that browser?!

Dabrowski

10:03 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If this second argument is omitted, all array elements from the start element to the end of the array are removed

Didn't realise that, but I'd still say the W3Schools is correct, and that is a side effect.

Infact, just after I wrote that I looked it up on Mozilla's developer website:
[developer.mozilla.org...]

The 2nd argument is not optional.

Scally_Ally

9:09 am on May 2, 2007 (gmt 0)

10+ Year Member



Cool,
Thanks for clearing that up for me.

Ally