By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Prompt window - OK/Cancel in Do While

P: 18
My question is closely related to and was not answered there, so I figured I'd revive it since I think I have the same issue.

Here is my applicable code:
Expand|Select|Wrap|Line Numbers
  1.   do {
  2.    var f=prompt('Would you like the output to be displayed in this page (1) or as a pop-up (2)','1');
  3.     }
  4.   while (f != "1" && f != "2" && f != "");
  6. // Begin case statement to make the code do its stuff based on "f"
Basically I want the prompt to get the user to have to put in either a 1 or a 2 no matter what, unless they hit Cancel. That means if they erase my "1" I have in there by default and they leave the prompt blank and they hit OK, I want it to spit the prompt back at them. The code above works great for that except the Cancel will just make the box stay up. I know that it's because of my f != "" above (the element that checks to see if the box was left blank), but is there a way to distinguish between when f happens to be null from being left blank (f's value is actually "undefined") and when the Cancel button was pressed (when f's value is "null")? Is there actually a difference? Using f !="null" above does not work, either.

Aug 28 '07 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 1,351
try this:

Expand|Select|Wrap|Line Numbers
  1. var f = "";
  2. do {
  3. f=prompt('Would you like the output to be displayed in this page (1) or as a pop-up (2)','1');
  4. }
  5. while (f != "1" || f != "2" || f == "");
i might have missed read your question, but does that do what you want?

good luck
Aug 28 '07 #2

Expert 100+
P: 173
Hi Tom,

When cancel is pressed the returned value (f) is null. This can be seen by changing the code to:
Expand|Select|Wrap|Line Numbers
  1. do {
  2. var f=prompt('Would you like the output to be displayed in this page (1) or as a pop-up (2)','1');
  3. alert(f);
  4. }
  5. while (f != "1" && f != "2" && f != "");
  7. // Begin case statement to make the code do its stuff based on "f"
The additional line simply alerts you to the value if f so that you can see what the output is. The code should work as you wish using:
Expand|Select|Wrap|Line Numbers
  1. do {
  2. var f=prompt('Would you like the output to be displayed in this page (1) or as a pop-up (2)','1');
  3. }
  4. while (f != "1" && f != "2" && f != null);
  6. // Begin case statement to make the code do its stuff based on "f"
The other effect is that the prompt is not removed when the content is deleted as previously.
Aug 28 '07 #3

Expert 5K+
P: 5,821
Heya, Tom. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 28 '07 #4

P: 18
epots9, not quite - the prompt never ends when you have "or" statements between it all. Thanks for trying, though.

Mods - sorry - forgot to look at that section...

But phvfl, you're awesome, thank you - your code worked great. I had posted an alert on "f" before and it told me what the values of it were and all, but I thought I had to have quotes on the "null" - guess I didn't! Was beating my head up against the wall on this for too long to see the obvious. Thanks again, all.

Aug 28 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.