GetDotted Domains

Viewing Thread:
"JavaScript Question"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Sun 08/12/02 at 17:00
Regular
Posts: 787
Hey

I'd like to know how you can create a variable inside a function and then use it inside a different function. for example...

function getName()
{
var name=prompt("your name","")
}
function alertNamed()
{
alert(name)
}

Very simple example, and I know it doesn't work. I know it's because the name variable is local, but I know you can declare a variable and then use it inside other functions. It has something to do with return variablename or something similar...

I know it's a really basic quesiton but I've scoured the web and can't find an answer anywhere.

Help?

Cheers :-)
Sun 08/12/02 at 19:40
Regular
"It goes so quickly"
Posts: 4,083
I see how you ment, but I'm not sure how you could do that.

I was hoping the 'global' bit would make the variable available to other functions, rather than just passing it.

I would be something like this, I think.

function alertNamed('name') {
alert(name);
}

but I'm not sure how to get the variable out of the first function, unless you call alertNamed(name) from inside the function .... but I was under the impression you don't want that???
Sun 08/12/02 at 17:43
Regular
"Picking a winner!"
Posts: 8,502
You'll need to pass the value of the variable from one function over to the next.
No idea how to do it in JS though :-)

go on MSN and I'll annoy you about it.
Sun 08/12/02 at 17:40
Regular
"You've upset me"
Posts: 21,152
Actually I think you may have misunderstood me, CJH...

What I need to do is create a variable inside a function and then pass it to another function. I'm sure it involves putting the variable name in the function name like: function rar(a_variable)
{
some stuff to decide the variable value then...

return a_variable
}
function use_the_variable_I_created(a_variable)


I'm sure it's something along those lines. I know I'm not maiing myself crystal clear though...
Sun 08/12/02 at 17:38
Regular
"You've upset me"
Posts: 21,152
Nope, global doesn't work.

Lana - Making it global would just compromise what I need it for as it needs to be made on the call of a function.
Sun 08/12/02 at 17:28
Regular
"It goes so quickly"
Posts: 4,083
I think ... *THINK* (not 100% sure) that you just add the word global to the beginning of the variable.

function getName() {
global var name = prompt("your name","")
}
Sun 08/12/02 at 17:28
Regular
"Picking a winner!"
Posts: 8,502
Not totally sure but I would imagine you have to make the variable global, instead of being local to the method it is in.
Are they going to be within the same bit of code?

why not declare the variable before the methods and see if that works.
Sun 08/12/02 at 17:01
Regular
"You've upset me"
Posts: 21,152
...and I know I used the phrase "I know" a lot in the last post ;-)
Sun 08/12/02 at 17:00
Regular
"You've upset me"
Posts: 21,152
Hey

I'd like to know how you can create a variable inside a function and then use it inside a different function. for example...

function getName()
{
var name=prompt("your name","")
}
function alertNamed()
{
alert(name)
}

Very simple example, and I know it doesn't work. I know it's because the name variable is local, but I know you can declare a variable and then use it inside other functions. It has something to do with return variablename or something similar...

I know it's a really basic quesiton but I've scoured the web and can't find an answer anywhere.

Help?

Cheers :-)

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Just a quick note to say thanks for a very good service ... in fact excellent service..
I am very happy with your customer service and speed and quality of my broadband connection .. keep up the good work . and a good new year to all of you at freeola.
Matthew Bradley
LOVE it....
You have made it so easy to build & host a website!!!
Gemma

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.