-2

Programming a simple web page but when i use JavaScript alert and prompt boxes I am randomly directed to another screen that says "file not found" This happens when I use the enter key to say "ok"on the Java Script prompt box and anytime I just click on the okay for the alert box. How can I fix this? why is this happening? HTML code

<!DOCTYPE html>
<html>
    <title>BasketBall Game</title>
    <link href ="styles.css" rel = "stylesheet" type ="text/css">
       <script type =  "text/javascript" src = myScript.js>
    </script>
<body>
    <div id = "mainbody">
    <h1>BaksetBall Game</h1>
       <form id="frm1" action="form_action.asp" onsubmit="play()">
            <!--<label>Please Enter in Your Team Name:</label><input id="myInput" type="text" name="fname" size="20" autofocus placeholder="i.e. BYU" value ="" onkeydown = "if (event.keyCode == 13) document.getElementById('playButton').click()"><br>-->
           <label>Please Enter in Your Team Name:</label><input id="myInput" type="text" name="fname" size="20" autofocus placeholder="i.e. BYU" value =""><br>
            <!--<button id = "playButton" type = "submit" onclick="play()" value = "Play"> Play </button> -->
           <button  id = "playButton" type = "submit" >Play </button>
           <button type = "button" onclick="reset()" value = "reset"> Reset</button><br>
        </form>

       <p id = "output"></p>
    </div>
    </body>
</html>





JAVASCRIPT CODE
//function that will go through and play games between user schools and other schools
function play()
{
    //get user input on their team name
    var x = document.getElementById("frm1");

    //create a new object of type Team and initialize values
    var userTeam = new Team(null, 0, 0); 

    //assign team name
    userTeam.Name = x.elements[0].value;

    //check to make sure the user enters in their team name
    if (userTeam.Name == "")
    {
        window.alert("Please enter in a team name");
        document.getElementById("myInput").select();
    }
    else
    {
        //initialize some variables
        var oppTeam = null;
        var games = null; 
        var homeScore = 0;
        var visitScore =0;

        //prompt for number of games to be played
        games = prompt("Please enter in how many games you want to play");

        //for loop to iterate through the games and keep track of userTeams wins and losses
        for (var i =0; i < games; i++)
        {
            oppTeam = window.prompt("Please enter in an opposing team");
            //while loop to check against ties
            while(homeScore == visitScore)
            {
                //call to random function to get scores between 0 and 100
                homeScore = getRandomInt(0,100);
                visitScore = getRandomInt(0,100);
            }

            //determine who wins and who losses, and keep track of data
            if(homeScore > visitScore)
            {
                userTeam.wins = userTeam.wins +1; 
            }

            else
            {
                userTeam.losses = userTeam.losses +1;
            }
        }
         //print out the results of the games
         document.getElementById("output").innerHTML = userTeam.Name +" Wins: "+ userTeam.wins + " Losses:"+ userTeam.losses;
     }
}

//function to get random numbers
function getRandomInt(min, max) 
{
  return Math.floor(Math.random() * (max - min)) + min;
}

//function to reset the game
function reset()
{
    document.getElementById("output").innerHTML = " ";
    document.getElementById("frm1").reset();
    document.getElementById("myInput").select();

}

//function to create the Team "class"
function Team(Name, wins, losses)
{
    this.Name = Name;
    this.wins = wins;
    this.losses = losses;
}
  • 1
    Please include your code in your question – jasonscript Apr 12 '16 at 04:29
  • Which browser are you using? Which OS? You may have some malware interference... Install & run some respectable anti-virus software; then Install a respectable browser, like Chrome, Firefox, or Opera. –  Apr 12 '16 at 04:30
  • Google Chrome Browser, Windows 10. I have done a virus scan and it comes up 100% clean – Bryce Regan LeFevre Apr 12 '16 at 05:01
  • What is form_action.asp doing? You are submitting your form to this page. I assume that page is sending you elsewhere? – tvdpol Apr 12 '16 at 05:27
  • yes, so the program works when i use my mouse to click on the buttons but when I press the enter key it sends me to another page web address: file:///C:/Users/Bryce/Desktop/is303Project4/form_action.asp?fname= and text that reads Your file was not found It may have been moved or deleted. ERR_FILE_NOT_FOUND – Bryce Regan LeFevre Apr 12 '16 at 05:30
  • Do you use a server to test it or are you running it just from your file system? file:// is not expected. It should be something like http:// or https:// – tvdpol Apr 12 '16 at 05:39
  • no its just an HTML file on my desktop in a folder with the javascript file and a css file. I just open the file in google chrom – Bryce Regan LeFevre Apr 12 '16 at 05:43
  • There you have your problem. The submit expects a server to handle your request. So for it to work you have to upload your project to a ASP.net development server or setup one locally. – tvdpol Apr 12 '16 at 05:49
  • Thanks so much! I also realized that I didn't need the action attribute inside of the form, after I replaced that with onsubmit = "return false" and changed the button type to button instead of submit it started working just fine, thanks so much! – Bryce Regan LeFevre Apr 12 '16 at 06:04
  • Retina false in your functions , otherwise your action is called – atrebbi Apr 12 '16 at 06:20

1 Answers1

0

As stated in the comments:

The submit expects a server to handle your request. So for it to work you have to upload your project to a ASP.net development server or setup one locally.

tvdpol
  • 198
  • 2
  • 13