53

I'm calling a Javascript window.prompt() and prompting the user to submit a variable which I compare to another variable (a very basic password protection). The function works great, however, if you click "cancel" on the prompt() window, the function does not simply terminate, but rather compares the variable to an empty string, (which the user opted not to submit by pressing "cancel" instead) resulting in the function continuing to the else{ } portion.

My question is, how do I terminate the function upon pressing cancel? I just need to know how to target the cancel button.

Usually I would just call a .stop() on the click() of a button, but I don't know how to target the prompt-window's cancel button.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Justin Ward
  • 825
  • 1
  • 7
  • 14

6 Answers6

116

prompt returns a string if the user presses OK ('' being with no value submitted). If the user pressed Cancel, null is returned. All you need to do is check whether the value is null:

function doSomething() {
    var input;
    input = prompt('Do something?');
    if (input === null) {
        return; //break out of the function early
    }
    switch (input) {
    case 'fun':
        doFun();
        break;
    case 'boring':
        beBoring();
        break;
    }
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    Awesome! Thanks a ton for your help! Your explanation made it seem so obvious it was like a slap across the face! Haha. Thanks again. :) – Justin Ward Oct 13 '12 at 18:35
  • If this answered your question, be sure to select the checkmark next to it to mark it as an answer. – zzzzBov Oct 13 '12 at 19:53
  • Ok - I'm a noobie to this site, so I didn't know. – Justin Ward Oct 14 '12 at 20:27
  • @JustinWard, Then I recommend reviewing the [faq] if you haven't already. [Editing Help](http://stackoverflow.com/editing-help) is also useful. – zzzzBov Oct 15 '12 at 05:29
7

You should explicitly check for null as the return value (using triple-equals) and return when this is the result.

var result = prompt("OK?");
if (result === null) {
    return;
}

This allows you to distinguish from the empty string, which is what's returned when the user clicks OK but enters no content.

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • 1
    Awesome! Thanks a ton for your help! Exactly what I was looking for. Pretty damn simple, really. I completely forgot about returns of 'null'. – Justin Ward Oct 13 '12 at 18:33
4

One substantial problem with handling the result of 'prompt' is that Safari (at least version 9.1.2) returns "" instead of null when "Cancel" is clicked. This means that: if(result==null) return; does not work, and you cannot distinguish between entry of a null string, and cancellation.

David Crowe
  • 113
  • 6
2

Can you just check for

if (prompt_responce == null)

to tell if it is closed.

Michael Shaffer
  • 374
  • 2
  • 16
2

Try:

message = prompt("Enter text");
if(message == "null" || message == null || message == "" );

This worked for me.

Filnor
  • 1,290
  • 2
  • 23
  • 28
1

When the user clicks cancel, this code should check for it:

$message = prompt("Please enter xxx");
if($message){
  //compare logic
}else{
  // warn or simply return nothing
  return;
}