In most browsers (including older versions of Safari), the Javascript prompt function returns null when the user clicks “Cancel”, and the empty string if the user clicks “Ok” with nothing in the text box. But in Safari 5.1, it returns the empty string for both cases.
I used Safari’s “report a bug” feature to report it to Apple, but who knows when they might even acknowledge it much less fix it. Does anyone have a workaround?
I’ve managed to come up with a real workaround, since Safari added support for
showModalDialog()in 5.1. Awfully convenient, that.First, create a file with this content:
Then, for broken versions of Safari (there seems to be no way to feature-detect this without popping up a prompt and asking the user to hit “Cancel”, so you’ll probably have to do a User-Agent check), execute the following Javascript to replace
window.prompt:Of course, change the path
/js/safari-5.1-bugfix.htmlto the correct path to the above-created HTML file on your server. Unfortunately, we cannot use adata:URI as Safari apparently has another bug where it loseswindow.dialogArgumentsand ignoreswindow.returnValuefor dialogs withdata:URIs.You can then use
prompt()as you normally would.