Why variables not available?

G

Geoff Cox

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];

}
 
R

RobG

Geoff said:
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?
 
G

Geoff Cox

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>
 
S

Stephen Chalmers

'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.
 
G

geoffcox

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top