Click to See Complete Forum and Search --> : Prompt/confirm
Paul Jr
11-16-2003, 06:08 PM
I'm attempting to learn Java Script, and this has come up, 'cause I'm curios. :-P
What I want is for a prompt to occur, and if it's blank, or nothing is changed, or if the "Cancel" button is hit, a confirm box will pop up, saying "Please enter a name." Now, if they hit cancel, I want the page to close, but if they hit enter, I want the prompt to come back up. I'm a bit lost on how to incorporate the confirm box into the whole deal; that's where I'm stuck.
First, I'm surprised no one has answered your question! Anyway, glad you're learning... :)
if(confirm("This is a yes or no question. Do you agree?")){
var myQuestion = prompt("This is a question. What is your answer?","");
if(myQuestion != null){
alert("Hi, "+myQuestion);
} else {
self.close();
}
} else {
self.close();
}
[J]ona
Paul Jr
11-16-2003, 09:11 PM
That was excellent -- except it was backwards. The prompt comes first then the confirm. Well, I twiddled around with it a bit, and got exactly what I wanted. But the Confirm does not display if you hit "Cancel" on the prompt, and a dialog box comes up asking you if you want to close the window if you hit "Cancel" on the Confirm.
<script type="text/javascript">
var name = prompt("Please enter your name","here");
if(name == "" || name == "here") {
var accept = confirm("Please enter a name");
if(accept == true) {
window.refresh(); }
else { self.close(); }
}
</script>
That's what I have so far.
Thanks for your help, Jona, much appreciated.
"null" is the same as "cancel" to JavaScript. if(answer==null){ alert("You pressed cancel."); }
[J]ona
Paul Jr
11-16-2003, 09:18 PM
I'm not sure I follow...
Also, another snag I just found. If you hit Okay in the confirm box, the page does not refresh to allow you to re-enter your name.
***EDIT***
I changed window.refresh(); to window.reload(); . The first time I tried it, it came up with an error that said the object doesn't support this property or method.
Well, the reason my first script was backwards is because it's too complex when put into words, so I didn't even really understand it. :p I just put something together and hoped you'd figure it out. lol. Um... for refresh you use location.reload(); not window.refresh();
[J]ona
fredmv
11-16-2003, 09:23 PM
<script type="text/javascript">
var n = prompt('Enter your name:', '');
while(!n) n = prompt('Enter your name:', '');
alert('Hello there, ' + n + '.');
</script>
Paul Jr
11-16-2003, 09:31 PM
An, Fremv, excellent, works like a charm. Now, the only thing left, is to get the page to reload if "Cancel" is selected in the Confirm box... window.reload(); just throws an error, maybe it's where I placed it.
<script type="text/javascript">
var name = prompt("Please enter your name","here");
while(!name) name = prompt("Please enter your name:", "here");
if(name=="" || name=="here") {
var accept = confirm("Please enter a name");
if(accept == true) {
window.reload(); }
else { self.close(); } }
</script>
fredmv
11-16-2003, 09:36 PM
The thing is, in the code I provided, you really wouldn't need a confirm dialog to refresh the window based on it's result since it will automatically throw up another prompt dialog if the user doesn't provide a value.
Paul Jr
11-16-2003, 09:41 PM
Ah, but, the code you provided will throw up another prompt if the user selects "Cancel" from the prompt. If they just hit "Okay" without having entered anything, THEN the confirm box comes up, asking them to enter in a name, since they did not.
Sorry to sound needy, but I came up with another bug. If you select "Okay" from the prompt, so that the confirm box is called, and you select "Cancel" from the confirm box, which would close the window, another dialog box pops up saying that the webpage is trying to close the window. Then, it asks you if you want to close the window or not, you can just click "No" and it will close that box and keep the window open, thus entirely defeating the script's purpose.
Originally posted by Paul Jr
window.reload(); just throws an error, maybe it's where I placed it.
It's location.reload(); ;)
[J]ona
Paul Jr
11-16-2003, 09:45 PM
! :o
Gah, how embarrassing. :o
Fredmv has this under control, so I'll let him take over. ;)
[J]ona
fredmv
11-16-2003, 09:48 PM
Thanks Jona, but you were doing just fine. :D
Still need help Paul?
Paul Jr
11-16-2003, 09:56 PM
Unfortunately, yes, one more thing.
If you hit Okay in the prompt, without having entered anything, you get the confirm box. If you select Cancel in the confirm box, it should just close the window. But what it does, is bring up another dialog box which says that the webpage is trying to close the window, then it asks you if you want to close the window. You can select No, thus defeating the purpose of the whole script. Is there a way to over-ride this? Or get around it?
Yes you can override it, but I am not going to be the one to give you that script, as it exsists for the security and privacy of the user at the page.
[J]ona
Paul Jr
11-16-2003, 10:19 PM
Ah, what a quandry. Oh well, it was just a test anyway, part of my learning experience. It can be done, and that's good enough for me. The script does work, so that's all I care about.
Thanks for all your help, Jona, and Fredmv. Couldn't have done it without you. :)
You're welcome.
[J]one
Edit: Notice any irony in my post? :D
Paul Jr
11-16-2003, 10:26 PM
*Looks for some irony...does not see any.*
Uhhh...?
You called me Jone.
[J]ona
Paul Jr
11-16-2003, 10:34 PM
Ah, yes, I didn't catch that Jone in the post. :) Sorry about that.
fredmv
11-16-2003, 10:34 PM
No problem. ;)