Click to See Complete Forum and Search --> : output value
tiger66
08-05-2003, 10:25 PM
Hi
I am trying to display a value which I have stored in a variable
I wrote the code below. However, I am getting a "output" undefined error message. Where did I do wrong? Thanks
<head>
<script language=javascript>
function a{
blah blah...
var output = value;
}
</script>
</head>
<body>
<script language=javascript>
document.write(output);
</script>
</body>
Khalid Ali
08-05-2003, 10:35 PM
u need to make sure that your code executes in a logical flow
first you want to make sure that the unction a is executed and then the document.write
Exuro
08-05-2003, 10:42 PM
The problem is that output isn't a global variable. You need to declare it outside the function for it to be global, and then you also need to call the function before you do your document.write. So something like this:
<script language=javascript>
var output;
function a{
blah blah...
output = value;
}
</script>
tiger66
08-06-2003, 10:56 AM
My function a is the onLoad function. Does that mean that it is the first function to load?
I tried
<head>
<script language=javascript>
var output;
function a{
blah blah...
output = value;
}
</script>
</head>
<body>
<script language=javascript>
document.write(output);
</script>
</body>
it is not working either (instead of giving me error messge, it's outputing 'undefined'... anymore idea?
Thanks
Exuro
08-06-2003, 08:32 PM
Okay, like we said, you're not calling the function before the document.write(). What an onLoad does is it calls the element after everything else has loaded. What I'd recommend doing instead of calling the function from an onLoad, is call it just before the document.write. So something like this:
<body>
<script language=javascript>
a();
document.write(output);
</script>
</body>