Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Array.push() not working with IE.

Reply
Thread Tools

Array.push() not working with IE.

 
 
Daz
Guest
Posts: n/a
 
      11-30-2006
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[i].ret.o;
totalRetiredUnowned += books[i].ret.u;
totalRegularOwned += books[i].reg.o;
totalRegularUnowned += books[i].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.

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      11-30-2006
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


 
Reply With Quote
 
 
 
 
Daz
Guest
Posts: n/a
 
      11-30-2006

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.

 
Reply With Quote
 
JohnnySpasm
Guest
Posts: n/a
 
      08-17-2007
On 30 Nov 2006 14:53:10 -0800, "Daz" <(E-Mail Removed)> 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[i].ret.o;
> totalRetiredUnowned += books[i].ret.u;
> totalRegularOwned += books[i].reg.o;
> totalRegularUnowned += books[i].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[i];
}

return this.length;
};
//END

 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      08-17-2007
JohnnySpasm meinte:
> On 30 Nov 2006 14:53:10 -0800, "Daz" <(E-Mail Removed)> 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
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      08-17-2007
JohnnySpasm meinte:

[some meaningless IE bashing snipped]

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


--
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
response.redirect is not working but server.transfer is working gaurav tyagi ASP .Net 14 01-20-2006 04:22 AM
wifi not working on new hp, or not working after live update =?Utf-8?B?RHJhZ29ueA==?= Wireless Networking 1 10-01-2005 11:17 PM
ASP.NET client-side validation working, but button click not working Alan Silver ASP .Net 1 08-02-2005 03:50 PM
Cookies working on intranet but NOT working on Internet Martin Heuckeroth ASP .Net 5 04-01-2005 01:37 AM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM



Advertisments