Safari 5.1 Prompt() 函数和取消

发布于 2024-12-03 14:48:38 字数 235 浏览 1 评论 0原文

在大多数浏览器(包括旧版本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

霓裳挽歌倾城醉 2024-12-10 14:48:38
var res = prompt('hello?', '')
if (res === null || res === '' && isSafari && confirm('was that cancel?'))
    cancel
var res = prompt('hello?', '')
if (res === null || res === '' && isSafari && confirm('was that cancel?'))
    cancel
冰魂雪魄 2024-12-10 14:48:38

我很感激您的提醒 - 我没有注意到这种行为,但现在我发现您是对的。 Safari 5 表现正常,5.1 对于任一按钮单击都会返回一个空字符串,如果提示字段中存在空字符串。

我唯一的建议是首先在字段中放置一个空格字符。
只有“确定”按钮才会返回空格,取消按钮将返回 null 或空字符串。

var p=prompt('what dya say?',' ');
if(!p)// cancel was *probably* clicked
else if(p===' ')//ok was clicked with no input
else// there is user input

用户可以开始输入内容,将其删除,然后点击“确定”而不是取消,
但这种情况也可能取消。

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.

var p=prompt('what dya say?',' ');
if(!p)// cancel was *probably* clicked
else if(p===' ')//ok was clicked with no input
else// there is user input

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.

此生挚爱伱 2024-12-10 14:48:38

我设法想出了一个真正的解决方法,因为 Safari 添加了对 showModalDialog 的支持5.1 中的 ()。非常方便,那个。

首先,创建一个包含以下内容的文件:

<html>
<head>
<title>Prompt</title>
<script type="text/javascript">
function a(){
        if(window.dialogArguments.length > 0)
                document.getElementById('a').textContent = window.dialogArguments[0]+'\n\n';
        if(window.dialogArguments.length > 1)
                document.getElementById('b').value = window.dialogArguments[1];
        document.getElementById('b').focus();
}

function s(b){
        window.returnValue=b?document.getElementById('b').value:null;
        window.close();
}

function kp(e){
        if(!e.DOM_VK_ENTER) e.DOM_VK_ENTER=13;
        if(!e.DOM_VK_RETURN) e.DOM_VK_RETURN=13;
        if(!e.DOM_VK_ESCAPE) e.DOM_VK_ESCAPE=27;

        switch(e.keyCode){
          case e.DOM_VK_ENTER:
          case e.DOM_VK_RETURN:
            if(e.preventDefault) e.preventDefault();
            if(e.stopPropagation) e.stopPropagation();
            e.returnValue = false;
            e.cancelBubble = true;
            s(1);
            return false;

          case e.DOM_VK_ESCAPE:
            if(e.preventDefault) e.preventDefault();
            if(e.stopPropagation) e.stopPropagation();
            e.returnValue = false;
            e.cancelBubble = true;
            s(0);
            return false;

          default:
            return true;
        }
}
</script>
<body style="text-align:center;white-space:pre-wrap" onload="a()">
<span id="a"></span>
<input type="text" id="b" onkeydown="return kp(event)" /><input type="button" value="Ok" onclick="s(1)" /><input type="button" value="Cancel" onclick="s(0)" />
</body>
</html>

然后,对于 Safari 的损坏版本(如果不弹出提示并要求用户点击“取消”,似乎无法对其进行功能检测,因此您可能必须进行用户代理检查),执行以下Javascript来替换window.prompt

(function(){
        if(window.console && window.console.log)
                window.console.log('Applying bugfix for Safari 5.1\'s prompt()');
        var oldprompt = window.prompt;
        window.prompt = function() {
                return showModalDialog(location.protocol+'//'+location.host+'/js/safari-5.1-bugfix.html', arguments);
        };
        window.prompt.$orig = oldprompt;
})();

当然,将路径/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:

<html>
<head>
<title>Prompt</title>
<script type="text/javascript">
function a(){
        if(window.dialogArguments.length > 0)
                document.getElementById('a').textContent = window.dialogArguments[0]+'\n\n';
        if(window.dialogArguments.length > 1)
                document.getElementById('b').value = window.dialogArguments[1];
        document.getElementById('b').focus();
}

function s(b){
        window.returnValue=b?document.getElementById('b').value:null;
        window.close();
}

function kp(e){
        if(!e.DOM_VK_ENTER) e.DOM_VK_ENTER=13;
        if(!e.DOM_VK_RETURN) e.DOM_VK_RETURN=13;
        if(!e.DOM_VK_ESCAPE) e.DOM_VK_ESCAPE=27;

        switch(e.keyCode){
          case e.DOM_VK_ENTER:
          case e.DOM_VK_RETURN:
            if(e.preventDefault) e.preventDefault();
            if(e.stopPropagation) e.stopPropagation();
            e.returnValue = false;
            e.cancelBubble = true;
            s(1);
            return false;

          case e.DOM_VK_ESCAPE:
            if(e.preventDefault) e.preventDefault();
            if(e.stopPropagation) e.stopPropagation();
            e.returnValue = false;
            e.cancelBubble = true;
            s(0);
            return false;

          default:
            return true;
        }
}
</script>
<body style="text-align:center;white-space:pre-wrap" onload="a()">
<span id="a"></span>
<input type="text" id="b" onkeydown="return kp(event)" /><input type="button" value="Ok" onclick="s(1)" /><input type="button" value="Cancel" onclick="s(0)" />
</body>
</html>

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:

(function(){
        if(window.console && window.console.log)
                window.console.log('Applying bugfix for Safari 5.1\'s prompt()');
        var oldprompt = window.prompt;
        window.prompt = function() {
                return showModalDialog(location.protocol+'//'+location.host+'/js/safari-5.1-bugfix.html', arguments);
        };
        window.prompt.$orig = oldprompt;
})();

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 a data: URI as Safari apparently has another bug where it loses window.dialogArguments and ignores window.returnValue for dialogs with data: URIs.

You can then use prompt() as you normally would.

迟到的我 2024-12-10 14:48:38

我尝试过,这对我有用

    var location="";
    var p=prompt("type your location",location ); 

if(p!==null)   {    location = p;    alert("ok do query");   }

I tryed and this worked for me

    var location="";
    var p=prompt("type your location",location ); 

if(p!==null)   {    location = p;    alert("ok do query");   }
与往事干杯 2024-12-10 14:48:38

当然,事实上是两个。

第一个明显的问题是,重新设计您的系统,使空字符串不是有效的响应。想一想,在谈话中,一个人在真正回答你的问题时茫然地盯着你看,这可以接受吗?

第二种是使用模式对话框(例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文