Safari 5.1 Prompt() 函数和取消
在大多数浏览器(包括旧版本的 Safari)中,当用户单击“取消”时,Javascript prompt
函数返回 null
,如果用户单击“确定”,则返回空字符串文本框中没有任何内容。但在 Safari 5.1 中,这两种情况都会返回空字符串。
我使用 Safari 的“报告错误”功能向 Apple 报告了该问题,但谁知道他们什么时候会承认这一问题,更不用说修复它了。有人有解决方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我很感激您的提醒 - 我没有注意到这种行为,但现在我发现您是对的。 Safari 5 表现正常,5.1 对于任一按钮单击都会返回一个空字符串,如果提示字段中存在空字符串。
我唯一的建议是首先在字段中放置一个空格字符。
只有“确定”按钮才会返回空格,取消按钮将返回 null 或空字符串。
用户可以开始输入内容,将其删除,然后点击“确定”而不是取消,
但这种情况也可能被取消。
I appreciate the heads up- I hadn't noticed the behavior, but now I see that you are correct. Safari 5 behaves normally, 5.1 returns an empty string for either button click, if there is an empty string in the prompt field.
My only suggestion is to put a space character in the field to start with.
Only the 'OK' button will return the space, cancel will return null or the empty string.
The user could start to enter something, delete it, and then hit 'ok' instead of cancel,
but that kind of case may as well be a cancel.
我设法想出了一个真正的解决方法,因为 Safari 添加了对
showModalDialog 的支持5.1 中的 ()
。非常方便,那个。首先,创建一个包含以下内容的文件:
然后,对于 Safari 的损坏版本(如果不弹出提示并要求用户点击“取消”,似乎无法对其进行功能检测,因此您可能必须进行用户代理检查),执行以下Javascript来替换
window.prompt
:当然,将路径
/js/safari-5.1-bugfix.html
更改为正确的路径上面在您的服务器上创建的 HTML 文件。不幸的是,我们无法使用data:
URI,因为 Safari 显然有 < em>另一个错误,它会丢失window.dialogArguments
并忽略带有data:
URI的对话框的window.returnValue
。然后您可以像平常一样使用
prompt()
。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.html
to 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.dialogArguments
and ignoreswindow.returnValue
for dialogs withdata:
URIs.You can then use
prompt()
as you normally would.我尝试过,这对我有用
I tryed and this worked for me
当然,事实上是两个。
第一个明显的问题是,重新设计您的系统,使空字符串不是有效的响应。想一想,在谈话中,一个人在真正回答你的问题时茫然地盯着你看,这可以接受吗?
第二种是使用模式对话框(例如 jQuery 对话框)来请求此信息。您将获得对它的完全控制,并且该应用程序看起来不像是 1996 年开发的。
Sure, two in fact.
First the obvious one, redesign your system so an empty string is not a valid response. Think about it, when in a conversation is it acceptable for one guy to stare at you blankly in actual answer to your question?
The second one is to use modal dialogs, like jQuery ones, to ask for this information. You'll get complete control over it and the application won't look like it was made in 1996.