...

View Full Version : x is undefined



Mr J
07-16-2004, 08:54 AM
Function test1 runs and alerts the argument values but when function 2 runs I get "x is undefined".
If I declared the variables x y z either global or local I get an empty alert.

Can someone educate me here

<script>
function test1(x,y,z){
alert(x+" "+y+" "+z)

setTimeout("test2(x,y,z)",2000)

}


function test2(x,y,z){
alert(x+" "+y+" "+z)
}

</script>




<a href="#null" onclick="test1(1,2,3)">Test 1</a>

jamescover
07-16-2004, 09:15 AM
sorry, where are your definitions?



http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html



undefined is a top-level property and is not associated with any object.

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.









-james

glenngv
07-16-2004, 09:36 AM
Change this:

setTimeout("test2(x,y,z)",2000)

to:

setTimeout("test2("+ x + "," + y + "," + z + ")",2000);

In the case of string arguments:

setTimeout("test2('"+ x + "','" + y + "','" + z + "')",2000);

Not so readable, right?
The same can be achieved without bothering for argument types using the function reference:

setTimeout(function(){test2(x,y,z)}, 2000);

But I think IE5 does not support that.

jamescover
07-16-2004, 09:42 AM
setTimeout(function(){test2(x,y,z)}, 2000);

But I think IE5 does not support that.


It works in IE 5.0:

undefined undefined undefined
undefined undefined undefined


I think, you posted it wrong:

<script>
function test1(x,y,z){
alert(x+" "+y+" "+z)
}
setTimeout("test2(x,y,z)",2000); //outside the function


function test2(x,y,z){
alert(x+" "+y+" "+z)
}

</script>








-james

Mr J
07-16-2004, 09:52 AM
Thanks Glenngv works fine :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum