Click to See Complete Forum and Search --> : Global Variables inside a Function
said_fox
07-19-2003, 04:02 PM
hi
suppose the following code:
<html><head>
<script>
function mycolor {
var x;
x = prompt("enter the color name","");
}
</script>
</head>
<body onload="mycolor()">
<script>
document.write("the color is "+x);
</script>
the problem is the variable x is defined inside the function, so it's not global by default.
my question is:L
How can I decalre the variable to be global inside a function in JavaScript? :confused:
David Harrison
07-19-2003, 04:13 PM
You can just remove the line:
var x;
and when the variable x is created, (as a prompt in this case), it will be considered global.
said_fox
07-19-2003, 04:27 PM
I had done as you had told me, remove the line var x,.
An error message is appeared, it says that the variable x is undefined.
So what's the solution.?
the code is
<html>
<head>
<script>
function mycolor() {
//var x;
x = prompt("enter the color name","");
}
</script>
</head>
<body onload="mycolor()">
<script>
document.write("the color is "+x);
</script>
</body>
</html>
David Harrison
07-19-2003, 04:36 PM
The problem is that you have not put type="text/javascript" into either of your script tags. Try this script:
<html>
<head>
<title>Global Variable In Function</title>
<script type="text/javascript"><!--
function mycolor() {
//var x;
x = prompt("enter the color name","");
}
mycolor();
//--></script>
</head>
<body>
<script type="text/javascript"><!--
document.write("the color is "+x);
//--></script>
</body>
</html>
I have set it to run the function before the page loads because once it has loaded it will have already executed the script in the body and not understood it because the variable x will not be created until the function in the head is run.
If that makes any sense.
Also note the HTML comment tags at the start and end of the script's, this is to hide the script from browsers that will not understand it.
said_fox
07-19-2003, 04:49 PM
Ok :)
It had run correctly.
But I have a question.
What's the rule or the function of onLoad Event in the Body Tag?
I understand it as to call the function used as its value. is not it?!
So I think the function mycolor() is declared before the document.write() in the body have to be run. is not it?!
David Harrison
07-19-2003, 04:54 PM
This is the order things will run in:
First: The function is declared, BUT not run.
Second: The script in the body is run but there is no variable named x so the browser does not understand it.
Third: The page finishes loading and runs the function declared in tha head and in doing so creates a global variable named x. But because the script in the body has already been run, it's too late.
onload does exactly what is says. It does something when the page has FINISHED loading.
said_fox
07-19-2003, 10:32 PM
Thank you for the important correction of my understanding of onLoad event handler
:D
David Harrison
07-20-2003, 04:19 PM
Happy to help. :)
Kinda skimmed over it an I'm not sure if lavalamp really explained how to make a variable global. Basically, when defining a variable inside a function, if you use the var (ie. var x as apposed to x) it will not be global. Take this example:
<script type="text/javascript">
function myFunction() {
var x = "5"; // will not be global
y = "5"; //will be global
}Hope that helps...
Jellybean
12-22-2004, 08:49 AM
Hi,
I have a query.
<script type="text/javascript">
function myFunction1() {
var x = "5"; // will not be global
y = "5"; //will be global
}
function myFunction2() {
alert(y);
}
</script>
May I know what is the output?
Will there be a pop up menu with 5?
If not, can you please advise how can I achieve that?
Thanks.
Regards,
Jellybean
:D
Jellybean
12-22-2004, 09:17 AM
Hi,
Basically I have these 2 functions. Each being called on a different page.
The first page calls function prestr when user clicks a hyperlink and another function updateaddr_to is called in the pop up menu that appears after clicking the hyperlink mentioned earlier.
How is it possible for me to pass the value of textval from prestr to updateaddr_to?
I am thinking of declaring textval as a global variable such that it can be used in the updateaddr_to function. However, it doesn't work.
Guess I have to call function prestr again in updateaddr_to. However, I doubt it can be done, since document.replyauthor.to_email.value will generate an error...
function prestr() {
textval = document.replyauthor.to_email.value;
alert(textval);
}
function updateaddr_to(){
var listStr = textval;
nAddr = document.getElementsByName('address');
if (listStr != ''){
for (i=0; i<nAddr.length; i++)
{if (nAddr[i].checked == true){listStr += "," + nAddr[i].value}}}
else{
for (i=1; i<nAddr.length; i++)
{if (nAddr[i].checked == true){listStr += nAddr[i].value + ","}}
if (nAddr[nAddr.length].checked == true){listStr += nAddr[i].value}
}
opener.replyauthor.to_email.value = listStr;
self.close();
}
I am very confused now.
Any advise is most appreciated.
Thanks for reading.
Regards,
Jellybean
:confused: