Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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