Safari 5.1 prompt()函数并取消 [英] Safari 5.1 prompt() function and cancel

查看:171
本文介绍了Safari 5.1 prompt()函数并取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数浏览器(包括旧版本的Safari)中,当用户点击时,Javascript 提示函数返回 null 取消,如果用户在文本框中没有任何内容的情况下点击确定,则为空字符串。但是在Safari 5.1中,它会为两种情况返回空字符串。

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.

我使用Safari的报告错误功能向Apple报告,但谁知道它们何时可能甚至承认它更少解决它。有没有人有解决方法?

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?

推荐答案

我设法提出了一个真正的解决方法,因为Safari增加了对 showModalDialog() 。非常方便,那。

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>

然后,对于破碎的Safari版本(似乎没有办法在没有弹出的情况下检测到这一点提示并要求用户点击取消,这样您可能需要进行用户代理检查),执行以下Javascript来替换 window.prompt

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;
})();

当然,更改路径 /js/safari-5.1-bugfix。 html 到服务器上面创建的HTML文件的正确路径。很遗憾,我们无法使用 数据: URI 因为Safari显然有另一个错误,它失去 window.dialogArguments 并忽略 window.returnValue 对于带有数据的对话框: URI。

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.

然后可以使用 prompt()和平常一样。

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

这篇关于Safari 5.1 prompt()函数并取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆