Reputation: 62145
Is it okay to call prompt this way:
prompt('Enter your text here');
instead of:
prompt('Enter your text here', '');
I.e. without passing a suggested input to it?
Upvotes: 3
Views: 336
Reputation: 5262
I thought the answer was 'yes', until I just saw that in IE7, this will result in having 'undefined' prefilled in the input box instead. Try it out for yourself in IE7 with a quick JSFiddle: http://jsfiddle.net/ALw6r/
Edit: from the comments, seems this is also broken in IE8.
Upvotes: 3
Reputation: 30481
Yes, it is OK.
The second parameter to prompt
method is optional (see "window.prompt on MDN").
As according to ECMAScript specification (ECMA-262, section 4.3.9), a value of undefined
is given to a variable which has no assigned value. In the prompt
method, it doesn't matter whether you leave the second parameter as undefined
or pass an empty string to it: both result in empty string as default value in the prompt.
In case you are wondering why this information is not available on DOM standards such as W3C DOM the answer is that it is a non-standard method that is "just" commonly supported by browsers (part of so called "DOM Level 0" spec). However, the upcoming HTML 5 is likely to define prompt (window.prompt
) as a standard method (see "6.4 User prompts").
Upvotes: 6
Reputation: 1074028
Yes, the second parameter is optional according to the HTML5 spec (the closest thing we currently have to a specification of prompt
and alert
and such):
The
prompt(message, default)
method, when invoked, must release the storage mutex, show the given message to the user, and ask the user to either respond with a string value or abort. The user agent must then pause as the method waits for the user's response. The second argument is optional. If the second argument (default
) is present, then the response must be defaulted to the value given by default. If the user aborts, then the method must returnnull
; otherwise, the method must return the string that the user responded with.
(My emphasis)
Upvotes: 3
Reputation: 29965
Yes, that is valid JavaScript. As w3schools mentions on http://www.w3schools.com/jsref/met_win_prompt.asp it's optional
Upvotes: 0
Reputation: 75307
In the documentation on MDC, the second parameter is listed as optional.
value is a string containing the default value displayed in the text input field. It is an optional parameter.
Upvotes: 3