www.webdeveloper.com
Results 1 to 6 of 6

Thread: This works in Firefox and Chrome, but not in Safari

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    6

    This works in Firefox and Chrome, but not in Safari

    Hey,

    First post here. This is a simple game I created within my HTML5 doc. It's "Rock, Paper, Scissors." Can someone tell me why this code runs as it should in FF and Chrome, but not in Safari?

    The problem I'm having is that when I run the code, if the user hit's "cancel" before any text is submitted via the prompt, an alert dialogue should show up and say "That's cool." Otherwise, if any text is entered, and the user does not enter either rock, paper or scissors, the dialogue says "I said rock, paper or scissors!" In FF and Chrome, if the user cancels the initial prompt to play the game, it returns "false," the alert "That's cool." appears, and the loop stops. If this is run in Safari, the prompt "I said rock, paper or scissors!" appears.

    So...Safari skips the "That's cool" else/if when the initial prompt is cancelled.

    Thanks.

    <script type="text/javascript">
    function runGame()
    {
    var result = "";

    var player = prompt("Pick rock, paper, or scissors.");
    if (player !== null) {
    player = player.toLowerCase();
    }


    var choices = ["rock","paper","scissors"];
    var computer = choices[Math.floor(Math.random()*3)];

    var win = "Your "+player+" beats "+computer+". You win.";
    var winScissors = "Your "+player+" beat "+computer+". You win.";
    var lose = "Your "+player+" loses to "+computer+". You lose.";
    var loseScissors = "Your "+player+" lose to "+computer+". You lose.";
    var draw = "A draw: "+player+" on "+computer+".";

    if(player === "rock"){
    if(computer === "scissors"){
    result = win;
    }
    else if(computer === "paper"){
    result = lose;
    }
    else if(computer === "rock"){
    result = draw;
    }
    }
    else if(player === "paper"){
    if(computer === "rock"){
    result = win;
    }
    else if(computer === "scissors"){
    result = lose;
    }
    else if(computer === "paper"){
    result = draw;
    }
    }
    else if(player === "scissors"){
    if(computer === "paper"){
    result = winScissors;
    }
    else if(computer === "rock"){
    result = loseScissors;
    }
    else if(computer === "scissors"){
    result = draw;
    }
    }
    else if (player === null) {
    alert("That's cool.");
    return false;
    }
    else if (player !== "rock" || player !== "paper" || player !== "scissors") {
    result = ("I said rock, paper or scissors!");
    }
    var repeat = confirm(result + "\n\nDo you want to play again?");
    if (repeat === false) {
    alert("Thanks for playing!");
    }
    else {
    runGame();
    }
    }
    </script>

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    After a cancel, an alert(typeof(player)) shows an object with IE (whitout HTML5) and FireFox, but a string with FireFox. Replace if (player === null) by if (!Boolean(player)) which is always true with null "" or 0. Since, It would be more practical to click on images and to see statistics...

  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Thanks! That kind of worked, but if it were to run exactly the way I wanted it to, hitting "cancel" on the first prompt would alert("That's cool."), and hitting "ok" with nothing typed would alert("I said Rock, Paper or Scissors!")

    Right now hitting both Cancel and OK on a blank prompt returns "That's cool." I can live with that though.

    Is there any way to distinguish between hitting OK with nothing typed and hitting "Cancel?"

  4. #4
    Join Date
    Mar 2012
    Posts
    6
    Is there any way to distinguish between hitting OK with nothing typed and hitting "Cancel?"

  5. #5
    Join Date
    Feb 2006
    Posts
    2,927
    Put a single space in the prompt's 2nd parameter and look for the single space
    to indicate the player pushed OK without changing it.

    You'll get an empty string from Safari on cancel, and null on most others.

    You'll want to strip any trailing or leading spaces from the input after you test for !player.

    var player = prompt("Pick rock, paper, or scissors."," ");
    Last edited by mrhoo; 03-30-2012 at 12:29 AM.

  6. #6
    Join Date
    Mar 2012
    Posts
    6
    Thanks that worked. It's kind of hack almost but hey, mission accomplished

    Now can anyone tell me why Firefox reloads the page, bringing me back to the home page after the JavaScript game finishes?

    Quote Originally Posted by mrhoo View Post
    Put a single space in the prompt's 2nd parameter and look for the single space
    to indicate the player pushed OK without changing it.

    You'll get an empty string from Safari on cancel, and null on most others.

    You'll want to strip any trailing or leading spaces from the input after you test for !player.

    var player = prompt("Pick rock, paper, or scissors."," ");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles