/    Sign up×
Community /Pin to ProfileBookmark

Screwy Javascript

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?nnHere is an example…nnJustin”);

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?nnHere is an example…nn27”);
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>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 05.2005 — Delete all references to [B]nw[/B]. The inline [I]var nw=window.open();[/I], opens a window immediately.

The document.write will 'open' a new page when used in the way you have written your code.
Copy linkTweet thisAlerts:
@FatherSharkauthorSep 05.2005 — thank you for your able assistance, I appreciate the help.
×

Success!

Help @FatherShark spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...