Array.push() not working with IE.

Discussion in 'Javascript' started by Daz, Nov 30, 2006.

  1. Daz

    Daz Guest

    Hi everyone.

    First of all, I would be interested to know if anyone knows of a decent
    plugin for IE that validates JavaScript. I am running IE 5, 5.5 and 6
    through wine (Windows Emulator) on Linux, so I haven't yet found one.
    However, this is my problem:

    I have the following piece of script:

    // This is just test data...
    var books = Array(
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"A"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"B"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"C"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"D"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"E"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"F"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"G"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"H"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"I"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"J"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"K"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"L"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"M"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"N"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"O"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"P"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"Q"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"R"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"S"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"T"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"U"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"V"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"W"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"X"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"Y"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"Z"},
    {ret:{o:20,u:50},reg:{o:12,u:34},n:"0-9"}
    );

    function appendTotals()
    {
    var totalRetiredOwned;
    var totalRetiredUnowned;
    var totalRegularOwned;
    var totalRegularUnowned;
    for (var i = 0; i < books.length; i++)
    {
    totalRetiredOwned += books.ret.o;
    totalRetiredUnowned += books.ret.u;
    totalRegularOwned += books.reg.o;
    totalRegularUnowned += books.reg.u;
    }
    var totals =
    {ret:{o:totalRetiredOwned,u:totalRetiredUnowned},
    reg:{o:totalRegularOwned,u:totalRegularUnowned},n:"Totals"};
    books.push(totals);

    }
    appendTotals();

    IE seems to have a problem with the line that reads
    'books.push(totals);' When I comment it out, the rest of the script
    runs. When I leave it in, IE throws a wobbler, I get an error and the
    script stops. It's all well and good that IE reports that there's an
    error, but if only it told me what the fricking error was! I feel that
    Microsoft is laughing at me, along with the other users who have to
    seriously modify/hack/botch their otherwise, well written scripts in
    order to compensate for where IE has gone awry. Even when I change
    'books.push(totals);' to 'books.push("blah");', it still gets it's
    knickers in a twist. Perhaps JavaScript doesn't allow different data
    types in an array? I'm not 100% certain to be honest.

    Would anyone please be so kind as to point out where I might be going
    wrong? The entire script worls beautifully in Firefox, as always, it's
    IE that is 'the thorn in my side', (only replace 'thorn' with '12 inch
    spike').

    Many thanks in advance.

    Seriously confused.

    Daz.
     
    Daz, Nov 30, 2006
    #1
    1. Advertising

  2. Daz

    Matt Kruse Guest

    Daz wrote:
    > Even when I change
    > 'books.push(totals);' to 'books.push("blah");', it still gets it's
    > knickers in a twist.


    Early versions of IE (<=5?) didn't support Array.push.

    Simply create your own push function and add it to Array.prototype to add
    this functionalty for any browser that doesn't already have it.
    Google it to find examples.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Nov 30, 2006
    #2
    1. Advertising

  3. Daz

    Daz Guest

    Matt Kruse wrote:

    > Daz wrote:
    > > Even when I change
    > > 'books.push(totals);' to 'books.push("blah");', it still gets it's
    > > knickers in a twist.

    >
    > Early versions of IE (<=5?) didn't support Array.push.
    >
    > Simply create your own push function and add it to Array.prototype to add
    > this functionalty for any browser that doesn't already have it.
    > Google it to find examples.
    >
    > --
    > Matt Kruse
    > http://www.JavascriptToolbox.com
    > http://www.AjaxToolbox.com


    You're a star Matt! Many thanks for your input. It had never occured to
    me that a browser wouldn't support such a basic function.
     
    Daz, Nov 30, 2006
    #3
  4. Daz

    JohnnySpasm Guest

    On 30 Nov 2006 14:53:10 -0800, "Daz" <> wrote:

    >Hi everyone.
    >
    >First of all, I would be interested to know if anyone knows of a decent
    >plugin for IE that validates JavaScript. I am running IE 5, 5.5 and 6
    >through wine (Windows Emulator) on Linux, so I haven't yet found one.
    >However, this is my problem:
    >
    >I have the following piece of script:
    >
    >// This is just test data...
    > var books = Array(
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"A"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"B"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"C"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"D"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"E"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"F"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"G"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"H"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"I"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"J"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"K"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"L"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"M"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"N"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"O"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"P"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"Q"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"R"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"S"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"T"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"U"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"V"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"W"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"X"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"Y"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"Z"},
    > {ret:{o:20,u:50},reg:{o:12,u:34},n:"0-9"}
    > );
    >
    > function appendTotals()
    > {
    > var totalRetiredOwned;
    > var totalRetiredUnowned;
    > var totalRegularOwned;
    > var totalRegularUnowned;
    > for (var i = 0; i < books.length; i++)
    > {
    > totalRetiredOwned += books.ret.o;
    > totalRetiredUnowned += books.ret.u;
    > totalRegularOwned += books.reg.o;
    > totalRegularUnowned += books.reg.u;
    > }
    > var totals =
    >{ret:{o:totalRetiredOwned,u:totalRetiredUnowned},
    >reg:{o:totalRegularOwned,u:totalRegularUnowned},n:"Totals"};
    > books.push(totals);
    >
    > }
    > appendTotals();
    >
    >IE seems to have a problem with the line that reads
    >'books.push(totals);' When I comment it out, the rest of the script
    >runs. When I leave it in, IE throws a wobbler, I get an error and the
    >script stops. It's all well and good that IE reports that there's an
    >error, but if only it told me what the fricking error was! I feel that
    >Microsoft is laughing at me, along with the other users who have to
    >seriously modify/hack/botch their otherwise, well written scripts in
    >order to compensate for where IE has gone awry. Even when I change
    >'books.push(totals);' to 'books.push("blah");', it still gets it's
    >knickers in a twist. Perhaps JavaScript doesn't allow different data
    >types in an array? I'm not 100% certain to be honest.
    >
    >Would anyone please be so kind as to point out where I might be going
    >wrong? The entire script worls beautifully in Firefox, as always, it's
    >IE that is 'the thorn in my side', (only replace 'thorn' with '12 inch
    >spike').
    >
    >Many thanks in advance.
    >
    >Seriously confused.
    >
    >Daz.


    IE is an absolute pain: it does like the opposite of everything you'd
    expect it to. To save you the time, or just for reference, here's a
    prototype function for Array.push:

    //START
    Array.prototype.push = Array.prototype.push || function() {
    var thisLength = this.length;

    for(var i = 0, argumentsLength = arguments.length; i
    arguments.length; i++)
    {
    this[thisLength + i] = arguments;
    }

    return this.length;
    };
    //END
     
    JohnnySpasm, Aug 17, 2007
    #4
  5. JohnnySpasm meinte:
    > On 30 Nov 2006 14:53:10 -0800, "Daz" <> wrote:
    >
    >> Hi everyone.
    >>
    >> First of all, I would be interested to know if anyone knows of a decent
    >> plugin for IE that validates JavaScript. I am running IE 5, 5.5 and 6
    >> through wine (Windows Emulator) on Linux, so I haven't yet found one.
    >> However, this is my problem:
    >>
    >> I have the following piece of script:
    >>
    >> // This is just test data...
    >> var books = Array(


    Interesting. However Arrays are instantiated with *new* Array() or just
    enclosing []. In your case books contains the result of a function named
    Array.

    >> {ret:{o:20,u:50},reg:{o:12,u:34},n:"A"},

    [snip]
    >> {ret:{o:20,u:50},reg:{o:12,u:34},n:"0-9"}
    >> );
    >>
    >> IE seems to have a problem with the line that reads
    >> 'books.push(totals);' When I comment it out, the rest of the script
    >> runs. When I leave it in, IE throws a wobbler, I get an error and the
    >> script stops.


    Perhaps IE gets it right this time. books is no array.
    (JFTR: array.push() is not supported by IE5.)

    >> Microsoft is laughing at me, along with the other users who have to
    >> seriously modify/hack/botch their otherwise, well written scripts in
    >> order to compensate for where IE has gone awry.


    Well, IE messes up a lot when it comes to its DOM, but so far Arrays
    were ok. Maybe MS is not to blame on this one.

    >> Perhaps JavaScript doesn't allow different data
    >> types in an array?


    It does.

    Gregor


    --
    http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://www.licht-blick.at ::: Forum für Multivisionsvorträge
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
     
    Gregor Kofler, Aug 17, 2007
    #5
  6. Gregor Kofler, Aug 17, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. XyZaa
    Replies:
    0
    Views:
    573
    XyZaa
    Jul 19, 2007
  2. Daniel Berger

    Testing array.push(array)

    Daniel Berger, Nov 9, 2005, in forum: Ruby
    Replies:
    2
    Views:
    122
    Daniel Berger
    Nov 9, 2005
  3. Replies:
    5
    Views:
    145
    YANAGAWA Kazuhisa
    Mar 8, 2006
  4. samppi
    Replies:
    27
    Views:
    496
    David A. Black
    Dec 5, 2007
  5. Amanda ..
    Replies:
    3
    Views:
    147
    Amanda ..
    Aug 25, 2008
Loading...

Share This Page