469,927 Members | 1,848 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Why variables not available?

Hello,

I have the code below on the first page and the value of the "name"
variable is available on the second page but the firstname and
secondname values are not...

What am I misisng here?

Thanks,

Geoff

function prompter(button)

{

name=prompt('What\'s your name?',"Please enter your name here");
Response=alert("Welcome "+name);

both = name.split(' ');
firstname = both[0];
lastname = both[1];

}
Sep 27 '05 #1
4 1196
Geoff Cox wrote:
Hello,

I have the code below on the first page and the value of the "name"
variable is available on the second page but the firstname and
secondname values are not...

What am I misisng here?
What we are missing is how you got 'name' to the second page. Whatever
variables you create in one page are completely destroyed when a new
page loads, even though you are creating them as globals.
function prompter(button)
What calls the function? Does it return anything? Does some other
function do something with the global variables you create?

{

name=prompt('What\'s your name?',"Please enter your name here");
Why is 'name' created as a global variable?
Response=alert("Welcome "+name);
'Response' is assigned a value equal to whatever is returned from
alert(...), which is nothing, so 'Response' is undefined. What is the
point? If you want to show what was entered to the prompt, then:

alert("Welcome "+name);

does the job.

both = name.split(' ');
This will create a global variable called 'both' that is an array of the
split parts of name.
firstname = both[0];
lastname = both[1];
Now you have two more global variables. If no spaces were entered,
'lastname' will be undefined. If nothing at all is entered, firstname
will be '' and lastname will be undefined. If ' blah' is entered,
firstname and lastname will be ''.

What are you trying to do?

}

--
Rob
Sep 27 '05 #2
On Tue, 27 Sep 2005 23:03:47 +1000, RobG <rg***@iinet.net.au> wrote:
Geoff Cox wrote:
Hello,

I have the code below on the first page and the value of the "name"
variable is available on the second page but the firstname and
secondname values are not...

What am I misisng here?


Rob,

I have put the whole code of the first page below - I can then use the
value of the name variable on the second page using IE v6. Now how is
that possible?!

Geoff

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<HEAD>

<SCRIPT>

function prompter(button)

{

name=prompt('What\'s your name?',"Please enter your first and
last name here, eg Jane Green");

alert("Welcome "+name + " Now read how to answer the
questions and then move to the questions");

document.getElementById('move').innerHTML =
"<button ID='move' onclick='movetoquestions()'>Move to
questions</button>";

if (button.parentNode && button.parentNode.removeChild)
{
button.parentNode.removeChild(button);
}

}

function movetoquestions()

{
window.location.href="second_page.htm";
}

</SCRIPT>

</head>

<body>

<h2 align="center">Tests</h2>

<pre>

</pre>

<p align="center"><input type="button" value="Please give us your
name" onclick="prompter(this)"></p>

<h3 align="center">How to answer a typical question</h3>

etc etc

<SPAN ID="move"></SPAN>

</body>
</html>


Sep 27 '05 #3
Geoff Cox <ge*******@notquitecorrectfreeuk.com> wrote in message news:vo********************************@4ax.com...
On Tue, 27 Sep 2005 23:03:47 +1000, RobG <rg***@iinet.net.au> wrote:
Geoff Cox wrote:
Hello,

I have the code below on the first page and the value of the "name"
variable is available on the second page but the firstname and
secondname values are not...

What am I misisng here?


'name' is effectively a reserved word as the variable window.name is created automatically, but is not read-only.
Use more imaginative names for variables.

--
S.C.

Sep 27 '05 #4
> I have the code below on the first page and the value of the "name"
> variable is available on the second page but the firstname and
> secondname values are not...
>
> What am I misisng here?


'name' is effectively a reserved word as the variable window.name is created automatically, but is not read-only.
Use more imaginative names for variables.


Stephen,

Regret have only just had my attention drawn to your reply. I am not
seeing it using Agent for some reason.

Are you saying that I can use the "name" (variable) value in the second
page because of window.name ?

Geoff


--
S.C.


Sep 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by aa | last post: by
13 posts views Thread by Larry L | last post: by
4 posts views Thread by Torsten Bronger | last post: by
9 posts views Thread by Tony Johansson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.