Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Screwy Javascript Problem


  • Please log in to reply

#1
FatherShark

FatherShark

    Member

  • Member
  • PipPip
  • 16 posts
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>
  • 0

Advertisements


#2
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
hoollly cow!!!
what a mess, i couldnt see what is where? i lost in your java script(this is not netscape version, you must give it your name and publish it)...just joking

but i recomend you that use if else statements more clear and organized this is first recomendation, second one is always check your data types example i can enter number the name field and also for age field give chace to user for cancel...
make your work clean for maintaniblity, it is going to make it for you to fix problems much more easy...
and always try to seperate your jscript code from html source, also for css too
example you can create file for each of them seperately.....that time you ll have a jscript pool and css pool you only need to invoke whatever you need for your page.

solution is :working version

<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;
/////////////////////from here to


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";
}
var nw=window.open();
nw.document.write(text);//here
}
}

onload=getName;
-->
</script>

</body>

</html>


take care
BuRnEr
  • 0

#3
FatherShark

FatherShark

    Member

  • Topic Starter
  • Member
  • PipPip
  • 16 posts
thanks for the able assist. I really appreciate it!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP