Question: What value does prompt() return if the user clicked the Cancel button?
Answer: The return value of canceled prompt() depends on the browser.
In most browsers the return value is null;
however, some very old browsers (e.g. early versions of IE) used to return '' (an empty string).
Therefore, when calling prompt(),
you may want to use an if statement like this:
userInput = prompt('Prompt text','Suggested input');
if (userInput != '' && userInput != null) {
// do something with the input
}
This code can be simplified to:
userInput = prompt('Prompt text','Suggested input');
if (userInput) {
// do something with the input
}
Copyright © 1999-2011, JavaScripter.net.