简体   繁体   中英

inconsistent blocking behaviour of window.prompt on mobile device

<script type="text/javascript">
    var x = prompt("enter x");
    var y = prompt("enter y");
    alert(x + " " + y);
</script>

This very simple code prompts the user for x , then for y and then displays an alert with both values. On desktop browsers this works fine because window.prompt and window.alert are blocking.

When viewing via a mobile device (user agent at the end of this post) the first prompt blocks but when I hit "OK" the second prompt is not blocking so the alert pops imediatelly with the following message (assume I entered 5 on the first prompt):

5 null

Why does this happen? Can it be solved?

Live example: http://jsfiddle.net/YEA5w/

Mozilla/5.0 (Linux; U; Android 2.3.5; en-gr; HTC_WildfireS_A510e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

For the record, on my Samsung Galaxy S2 both on the stock and Firefox browsers, your fiddle works fine for me.

If you are still having issues, perhaps you could use setTimeout or setInterval to continuously check if these prompts have been filled in and do the alert if they have.

Something like:

<script>
    var x = false;
    var y = false;
    var timer = setInterval(function(){
            if(x !== false && y !== false) {
                 alert(x + " " + y);
                 clearInterval(timer);
            }
    },100);
    x = prompt("enter x");
    y = prompt("enter y");
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM