Prompt and alert under conditions

G

geotso

Hi.

I'm looking for a javascript for the following scenario:

The visitor clicks a button and a Prompt box appears, where:
1. He clicks the Cancel button, typing nothing and a new page (null.html)
opens. Or,
2. He gives a wrong answer and an Alert pops-up, with "This is not the right
answer!" in it. So, he clicks Alert's OK button and the Prompt box re-opens
automatically. However, after three wrong answers a new page (sorry.html)
opens. Or,
3. When he gives the correct answer a new page (correct.html) opens.

Can someone help me please? Or am I asking too much?
 
G

geotso

Andrew said:
2b. Whereupon he calls her, who (she) decodes the JS
and tells him the password.

2c. Site cracked! ;-)

I know, I know... However it isn't a site. Is just a book for my little son.
So, can you help me now please?

Thanks.

BTW, sorry for I'm late. There was a problem with my ISP...
 
L

Lee

geotso said:
Hi.

I'm looking for a javascript for the following scenario:

The visitor clicks a button and a Prompt box appears, where:
1. He clicks the Cancel button, typing nothing and a new page (null.html)
opens. Or,
2. He gives a wrong answer and an Alert pops-up, with "This is not the right
answer!" in it. So, he clicks Alert's OK button and the Prompt box re-opens
automatically. However, after three wrong answers a new page (sorry.html)
opens. Or,
3. When he gives the correct answer a new page (correct.html) opens.

Can someone help me please? Or am I asking too much?

<html>
<head>
<title>Demo</title>
<script type="text/javascript">
var attempts=3;
var question="What's your favorite color?";
var answer="blue";
function guard(){
while (attempts-->0) {
var response = prompt(question,"");
if (!response) {
location = "null.html";
}else if (response==answer) {
location = "correct.html";
} else {
alert("This is not the right answer!");
}
}
location="sorry.html";
}
</script>
</head>
<body onload="guard()">
<p>This page is blank</p>
</body>
</html>
 
G

geotso

Lee said:
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
var attempts=3;
var question="What's your favorite color?";
var answer="blue";
function guard(){
while (attempts-->0) {
var response = prompt(question,"");
if (!response) {
location = "null.html";
}else if (response==answer) {
location = "correct.html";
} else {
alert("This is not the right answer!");
}
}
location="sorry.html";
}
</script>
</head>
<body onload="guard()">
<p>This page is blank</p>
</body>
</html>

Lee,

Thank you very much for your try!

However:
I can't access the "correct.html, since after the right answer ("blue"), the
Prompt box pops-up again, the cursor looks busy, and the status bar tells me
that IE tries to open the "correct.html". Furthermore, even after three
right answers I go to the "sorry.html".

I have to click three times the Cancel before I go to "sorry.html". Can this
be done after only one click?

BTW I call the function from a button with onClick and not at the onLoad
time as in your example. Could this cause the described problems?

Thanks again for your patience!
 
L

Lee

geotso said:
Lee,

Thank you very much for your try!

However:
I can't access the "correct.html, since after the right answer ("blue"), the
Prompt box pops-up again, the cursor looks busy, and the status bar tells me
that IE tries to open the "correct.html". Furthermore, even after three
right answers I go to the "sorry.html".

I have to click three times the Cancel before I go to "sorry.html". Can this
be done after only one click?

The problem is that I dislike Internet Explorer so much
these days that I don't generally bother to test in it.

This version works in IE as well as in decent browsers:


<script type="text/javascript">
var attempts=3;
var question="What's your favorite color?";
var answer="blue";
var newpage="sorry.html";
function guard(){
while (attempts-->0) {
var response = prompt(question,"");
if (!response) {
newpage = "null.html";
break;
}else if (response==answer) {
newpage = "correct.html";
break;
} else {
alert("This is not the right answer!");
}
}
location=newpage;
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top