Array.push() not working with IE.

D

Daz

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.
 
M

Matt Kruse

Daz said:
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.
 
D

Daz

Matt said:
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.

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.
 
J

JohnnySpasm

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
 
G

Gregor Kofler

JohnnySpasm meinte:
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.)

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.

It does.

Gregor
 
G

Gregor Kofler

JohnnySpasm meinte:

[some meaningless IE bashing snipped]

Sorry. Just realised that I responded to a useless reply of a question
from last year.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top