Click to See Complete Forum and Search --> : Invalid Argument on close bracket (?)


JSchwarz
03-19-2003, 08:31 AM
This simple code from my onLoad event is generating an Invalid Argument; the error appears to be occuring on the for loop close bracket (?):

{
var obj=document;
var names=new Array();
var strnames="";
var i=0;
for (prps in obj) {
names[i++] = prps + ':\t' + obj[prps];
} // for (prps in obj)
}

If I put an alert immediately after the names[i++] assignment, I get the message about 80 times (indicating that the loop and assignment are working correctly). If I put the alert immediately after the for loop close bracket, it never executes and I get the Invalid Argument error. In other words, this:

{
var obj=document;
var names=new Array();
var strnames="";
var i=0;
for (prps in obj) {
names[i++] = prps + ':\t' + obj[prps];
alert("test"); } // for (prps in obj)
}

produces 80 "test" alerts followed by an error. And this:

{
var obj=document;
var names=new Array();
var strnames="";
var i=0;
for (prps in obj) {
names[i++] = prps + ':\t' + obj[prps];
} alert("test"); // for (prps in obj)
}

generates an error and no alert boxes. I know the assignment is working (I've test it with an alert), and the syntax seems flawless (right?).

Also note that if I redefine obj to be the first form in the document (i.e., var obj=document.forms[0]), then I get no errors!

Why on earth is this close bracket causing an error? Or what is causing the error? Any advice is appreciated.

Thanks in advance.
Jeff

cyberade
03-19-2003, 10:17 AM
I am only aware of one construct for the javascript 'for loop' and that is:

for (counter_start ; loop_condition ; counter_increment )
{ iterative statements
}

for example:

for(i = 0; i < 6; i++){
loop_statement
}

DaiWelsh
03-19-2003, 10:24 AM
JSchwarz,

I suspect that your loop is failing at a particular property, possibly one that you are not allowed to read or that does not evaluate to a string (latter is more likely I think).

Try adding an alert before the assignment to tell you what property it is looking at, then when you run the code, the last alert you see is the one that is failing.

for (prps in obj) {
alert("prps");
names[i++] = prps + ':\t' + obj[prps];
} // for (prps in obj)

In fact for easier debugging you could add a text box and write the value of prps into it in each loop, that way you dont have to acknowledge 80 alert boxes :)

HTH,

Dai

JSchwarz
03-19-2003, 10:47 AM
Well, I don't know how to create a text box (I'm only a few months into JavaScript), so I added an alert like this:

{
var obj=document;
var names=new Array();
var strnames="";
var i=0;
for (prps in obj) {
alert(prps);
names[i++] = prps + ':\t' + obj[prps];
}
}

When I do that, the last property displayed is 'fileUpdatedDate'. Note that if I change the alert to read

alert(prps+":\t"+obj[prps]);

then it dies on the property before 'fileUpdatedDate'.

So now I wonder why it can't be converted to a string? I thought all JavaScript objects and values can be converted to a string?

Also, why does this generate the same error:

var val= (prps=="fileUpdatedDate") ? obj[prps].toString : obj[prps];
alert(prps+":\t"+ val );

Does that mean that I don't have access to that property? How could that possibly be? Is there some JavaScript basics that I'm completely missing or something?

Thanks for your help.
Jeff

Vladdy
03-19-2003, 10:57 AM
What browser are you using? I tried your code in IE6.0 and it worked fine.

DaiWelsh
03-19-2003, 10:57 AM
I dont know the ins and outs but I dont think it is a safe assumption that all javascript objects can be shown as strings - though I could well be wrong :) - anyway, I have tried your code and it works error free for me, what broweser version are you using?

Also, where abouts in your page are you using this js? It may be that the page is not fully loaded which might affect it?

HTH,

Dai

JSchwarz
03-19-2003, 11:03 AM
I'm using IE6. I have run Windows Update for my Win2000, so I have the absolute latest.

Note also that I have a file upload control (well, 10 of them) on this form.