By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

Why variables not available?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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.