Prompt - Distinguishing empty-string from Cancel in Opera

J

John Geddes

Opera (9.60 at least) seems to return an empty string if the user
chooses Cancel from a Prompt box. That is different from IE6, FF3,
Chrome, which all return null.

I want to offer the user the chance to confirm an action (with the
option of entering some explanatory text - or of leaving the explanation
blank) or cancelling that action. In the non-Opera browsers, null
denotes Cancel, and an empty string denotes OK (but no text entered).

Any suggestions on how to work around this in Opera?

John Geddes
England
 
M

Martin Honnen

John said:
Opera (9.60 at least) seems to return an empty string if the user
chooses Cancel from a Prompt box. That is different from IE6, FF3,
Chrome, which all return null.

I want to offer the user the chance to confirm an action (with the
option of entering some explanatory text - or of leaving the explanation
blank) or cancelling that action. In the non-Opera browsers, null
denotes Cancel, and an empty string denotes OK (but no text entered).

Any suggestions on how to work around this in Opera?

Are you sure it is an empty string? I have tested with some Opera 9
versions (9.10 and (9.63) on Windows and they all seem to return the
value undefined when the cancel button is used in a prompt dialog.

So that way you should be able to do e.g.
var r = prompt('foo?', 'bar');
if (typeof r === 'string')
{
// user hit OK
}
else
{
// user hit Cancel
}
 
T

Thomas 'PointedEars' Lahn

John said:
Opera (9.60 at least) seems to return an empty string if the user
chooses Cancel from a Prompt box.

With

window.prompt();

Opera/9.63 (X11; Linux i686; U; en) Presto/2.1.1 returns `undefined' as does
Opera/9.60 (X11; Linux i686; U; en) Presto/2.1.1.

Note that if you use the return value in the argument for window.alert(),
the string representation of the return value will be used instead. Use
`typeof' to be relatively sure about the return type:

var r = window.prompt(), t = typeof r;
window.alert('"' + (t != 'unknown' ? r : '') + '" : ' + t);
That is different from IE6, FF3, Chrome, which all return null.

AFAIK some earlier versions of the former two UAs returned `false' then.
I want to offer the user the chance to confirm an action (with the
option of entering some explanatory text - or of leaving the explanation
blank) or cancelling that action. In the non-Opera browsers, null
denotes Cancel, and an empty string denotes OK (but no text entered).

It would appear that a strict comparison against the empty string is the
best approach.


PointedEars
 
J

John Geddes

Martin said:
Are you sure it is an empty string? I have tested with some Opera 9
versions (9.10 and (9.63) on Windows and they all seem to return the
value undefined when the cancel button is used in a prompt dialog.

So that way you should be able to do e.g.
var r = prompt('foo?', 'bar');
if (typeof r === 'string')
{
// user hit OK
}
else
{
// user hit Cancel
}

Brilliant - thanks so much to you, Martin, and to Thomas.

John Geddes
 
S

SAM

Le 12/27/08 1:36 PM, John Geddes a écrit :
Opera (9.60 at least) seems to return an empty string if the user
chooses Cancel from a Prompt box. That is different from IE6, FF3,
Chrome, which all return null.

I want to offer the user the chance to confirm an action (with the
option of entering some explanatory text - or of leaving the explanation
blank) or cancelling that action. In the non-Opera browsers, null
denotes Cancel, and an empty string denotes OK (but no text entered).


javascript:alert(prompt('question?','answer'))

browser : Cancel : OK
---------------+------------+----------
Firefox 3.0.5 : null : 'answer'
Safari 3.1.2 : null : 'answer'
Opera 9.0 : undefined : 'answer' why not
iCab 3.0.5 : null : 'answer'
iCab 4.0.1 : 'answer' : 'answer' bug
iCab 4.2.5 : 'answer' : 'answer' bug

Any suggestions on how to work around this in Opera?

null or undefined is almost the same thing
(to see if the prompt was canceled)


javascript:alert(!!prompt('question?','answer'))

browser : Cancel : OK
---------------+-----------+---------
Firefox 3.0.5 : false : true
Safari 3.1.2 : false : true
Opera 9.0 : false : true
iCab 3.0.5 : false : true
iCab 4.0 / 4.2 : true : true bug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top