Skip to content Skip to sidebar Skip to footer

IE <= 8 .splice() Not Working

I have some simple code you can see in my fiddle. It alerts properly in all browsers and IE9, but not IE8 or 7. var func = function( x ) { var slice = [].slice, args =

Solution 1:

The ECMAScript 3rd edition standard requires the second deleteCount argument:

Array.prototype.splice(start, deleteCount [, item1 [, item2[,...]]])

MSDN docs show that IE follows this standard:

arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])

Firefox's SpiderMonkey allows the second argument to be optional (as do other modern browsers):

array.splice(index , howMany[, element1[, ...[, elementN]]])
array.splice(index[, howMany[, element1[, ...[, elementN]]]])

Description:

howMany An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.

Sources:


Solution 2:

Splice has a required second argument:

http://jsfiddle.net/7kXxX/2/

pass = args.splice(1,2);

The optional second argument is an extension in newer browsers that assume the rest of the array if left undefined

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice

Slice would be more appropriate if you want elements from 1 - to the end, doesn't look like there is any reason to remove elements from args.


Solution 3:

My solution to the Array.prototype.splice in IE (read more here):

(function () {
    'use strict';
    var originalSplice = Array.prototype.splice;
    Array.prototype.splice = function (start, deleteCount) {
        // convert the weird, not-really-an-array arguments array to a real one
        var args = Array.prototype.slice.call(arguments);
        // IE requires deleteCount; set default value if it doesn't exist
        if (deleteCount === undefined) {
            args[1] = this.length - start;
        }
        // call the original function with the patched arguments
        return originalSplice.apply(this, args);
    };
}());

Solution 4:

var func = function (x) 
{
    alert ([].slice.call (arguments, 1))
}

func( 'a', 1, 2 );

Solution 5:

I'm not familiar with this particular issue, but have you tried Sugar.js? It has some methods that might work for you (I believe replacing splice with from would work).


Post a Comment for "IE <= 8 .splice() Not Working"