PDA

View Full Version : Screwy Javascript


FatherShark
09-05-2005, 08:20 AM
Hello all,

please view my completed Javascript snippet below. The intention is for the page to load, the two prompts to get information from the user, and then this input be used to document.write in a new page.

Everything is happening as it should, except the new page is loads immediately, and the document.write is happening later. I had intended it to happen after the two prompts.

Could someone please let me know how to fix this, it would be very much appreciated.

Regards,

Justin

<html>

<head>

<link rel="stylesheet" href="style.css"/>

</head>

<body>

<h1>Age Page</h1>

<br /><br />

<h2>Compare Ages</h2>

<p>My current age is 28 years old</p>

<script type="text/javascript">
<!--
var myAge=28;
var nw=window.open();


function getName()
{
yourName=prompt('what is your name','');
if((yourName=='')||(yourName==null)) {
alert("Is it really that difficult for you to enter your name?\n\nHere is an example...\n\nJustin");

getName();
}

else
{
getAge();
}
}

function getAge() {
yourAge=prompt('what is your age','');
if((isNaN(yourAge))||(yourAge=='')||(yourAge==null))
{
alert("Is it really that difficult for you to enter your age?\n\nHere is an example...\n\n27");
getAge();
}

else
{
if(yourAge==myAge)
{
text="Hi "+yourName+", you are the same age as me";
}

if(yourAge<myAge)
{
text="Hi "+yourName+", you are "+(myAge-yourAge)+" year(s) younger than me";
}

if(yourAge>myAge)
{
text="Hi "+yourName+", you are "+(yourAge-myAge)+" year(s) older than me";
}

nw.document.write(text);
}
}

onload=getName;
-->
</script>

</body>

</html>

pixelDepth
09-05-2005, 10:21 AM
It is doing it like that because A. You are using the onload event handler. & B. You have the script below the HTML.

Just place the script into head tags, and change the the call to the function so it is just getName().

Mr J
09-05-2005, 07:10 PM
Please try the following

<html>
<head>
<link rel="stylesheet" href="style.css"/>

<script type="text/javascript">
<!--
var myAge=28;

function getName(){
yourName=prompt('what is your name','')
if((yourName=='')||(yourName==null)){
alert("Is it really that difficult for you to enter your name?\n\nHere is an example...\n\nJustin")

getName()
}
else{
getAge()
}
}

function getAge(){
yourAge=prompt('what is your age','')
if((isNaN(yourAge))||(yourAge=='')||(yourAge==null)){
alert("Is it really that difficult for you to enter your age?\n\nHere is an example...\n\n27")
getAge()
}
else{

if(yourAge==myAge){
text="Hi "+yourName+", you are the same age as me";
}

if(yourAge<myAge){
text="Hi "+yourName+", you are "+(myAge-yourAge)+" year(s) younger than me";
}

if(yourAge>myAge){
text="Hi "+yourName+", you are "+(yourAge-myAge)+" year(s) older than me";
}

nw=window.open('','win1','left=200,top=100,width=300,height=200')
nw.document.write(text)
}
}

onload=getName
-->
</script>

</head>

<body>

<h1>Age Page</h1>

<br /><br />

<h2>Compare Ages</h2>

<p>My current age is 28 years old</p>

</body>
</html>

FatherShark
09-05-2005, 10:59 PM
I would like to thank you both for your assistance. The script works fine now!