Safari 5.1 prompt() function and cancel

后端 未结 5 866
抹茶落季
抹茶落季 2020-12-18 20:00

In most browsers (including older versions of Safari), the Javascript prompt function returns null when the user clicks \"Cancel\", and the empty s

5条回答
  •  隐瞒了意图╮
    2020-12-18 20:38

    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:

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

    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.

提交回复
热议问题