4

Consider this code:

function reason(id) {
    var reason = prompt("State reason");
    while (!reason.trim()) {
        reason = prompt("Please enter text");
    }
    document.getElementById(id).value = reason;
    return true;
}

It works perfectly fine, but when I want to get rid of the poppup by pressing escape for example, the function returns true because the form executes. What should I do to make it do nothing if I close/cancel the poppup?

lightning_missile
  • 2,821
  • 5
  • 30
  • 58
  • return false; should work for both the cases – Sudhansu Choudhary Jul 06 '15 at 17:51
  • 2
    `return reason;` probably works - prompt returns null on cancel. – NG. Jul 06 '15 at 17:52
  • possible duplicate of [Javascript prompt() - cancel button to terminate the function](http://stackoverflow.com/questions/12864582/javascript-prompt-cancel-button-to-terminate-the-function) – Cᴏʀʏ Jul 06 '15 at 17:53
  • @Cᴏʀʏ the asker of the question you linked specifically said he clicked the cancel button to cancel the alert, I didn't click any buttons, I just pressed escape which should get rid of the alert. – lightning_missile Jul 06 '15 at 17:56
  • 1
    @morbidCode clicking `Cancel` is equivalent to pressing `Esc` for `prompt` and `confirm` dialogs. – Motti Jul 06 '15 at 17:59
  • 1
    @morbidCode: True, but pressing escape effectively is invoking the "Cancel" action. Also, note that Safari also returns an empty string on Cancel -- it does not differentiate between cancelling and entering an empty string (via [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt)), so you could get stuck in an infinite prompt loop. – Cᴏʀʏ Jul 06 '15 at 18:00
  • @Cᴏʀʏ: *"Also, note that Safari also returns an empty string on Cancel"* Gak. If MDN is really right about that, it's Yet Another Reason not to use `prompt` (not that we were short of them). – T.J. Crowder Jul 06 '15 at 18:03
  • @T.J.Crowder: [Some more discussion](http://stackoverflow.com/questions/7351124/safari-5-1-prompt-function-and-cancel) on the Safari issue. "Gak" indeed! – Cᴏʀʏ Jul 06 '15 at 18:05
  • @Cᴏʀʏ: Doesn't do that on my iPhone, I get `null`: http://jsbin.com/pukunodatu Re that link: Safari 5.1 is ***ancient***. – T.J. Crowder Jul 06 '15 at 18:07
  • @T.J.Crowder: Well, that's good to know. Admittedly as someone who hasn't used a Mac/iOS in many years I'm happy I probably don't have to remember another browser quirk. Thanks for checking! – Cᴏʀʏ Jul 06 '15 at 18:09

1 Answers1

3

... the function returns true because the form executes. What should I do to make it do nothing if I close/cancel the poppup?

It depends entirely on how you call your reason function, but if you want reason to return false when prompt is cancelled, then:

function reason(id) {
    var reason = prompt("State reason");
    while (reason !== null && !reason.trim()) {  // *** Changed
        reason = prompt("Please enter text");
    }
    if (reason === null) {                       // *** Added
        return false;                            // *** Added
    }                                            // *** Added
    document.getElementById(id).value = reason;
    return true;
}

prompt returns null when you cancel it.

But again, it's up to what calls reason to do something appropriate with the true or false.


Side note: You can call your function and a variable inside it by the same name, but it's not a great idea. If it's a habit, you'll end up making writing recursive functions quite difficult...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875