Tuesday 15 April 2014

javascript - Why are neither functions getting called if one of them is uncommented? -


I have an external javascript file that has two functions and both should be called every time the page is loaded. However, neither if I comment on the line in the JS file which calls the tester () function, the run game () is called, but if the tester () is not commented, then neither runs . However, if I place the checker () function call after the runGame () call, the run game () works, but the tester () still does not work.

The examiner () himself, then maybe something that I did wrong in that ceremony, can someone tell me what I did?

html

  & lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" src = "rock_paper_scissors.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P id = "sports" & gt; Test & lt; / P & gt; & Lt; Br / & gt; & Lt; Input type = "button" value = "play again" onClick = "tester ()" & gt; & Lt; / Input & gt; & Lt; / Body & gt;   

rock_paper_scissors.js

  var info = document.getElementById ("sports"); Var tester = function () {document.getElementById ('game'). InnerHTML = 'hello'; //info.innerHTML("HI "); }; Var comparison = function (like 1, choice 2) {if (choice1 == choice2) document.write ("the result is a tie!"); Otherwise if (prefer1 == "rock") {if (liked 2 == "scissors") document. Written ("you win! Rock wins"); Else document.write ("computer wins! Wins paper"); } And if (like 1 == "paper") {if (choice2 == "rock") document.write ("you win! Paper wins"); Else document.write ("computer wins! Scissors wins"); } And if (like 1 == "scissors") {if (choice2 == "rock") document.write ("computer wins! Rock wins"); And documents Write ("You can win! Scissors win"); } And documentation. Write ("You did not choose any genuine choice"); }; Var Runtgam = function () {var userChoice = Prompt ("Do you choose rock, paper or scissors?"); Var Computer Charge = Math.Random (); If (Computer Choice <0.34) {computerChoice = "rock"; } And if (Computer Choice <= 0.67) {computerChoice = "paper"; } And {computerChoice = "scissors"; } Document.write ("& lt; p & gt;" + userChoice + "by you ); Document.write (" & lt; p & gt; Computer selected "+ computer Choice + "& lt; / p & gt;"); Compare (User Chice, Computer Charge);; Running Game (); Tester ();    

Are you sure the element exists with the ID "game"? [Edit: Just took a better look at your code - your script HEAD , Which means that it is executed before the game is loaded, So it certainly does not exist.] If it is not, information is zero , and innerHtml of null to check the javascript console to see the error will break your code.

You can also place your code in the game element (anywhere in the body and at the end of the body Traditionally, just close the body tag) or run your code with the body's load event handler, or an A Using material library, which is a ready event. / P>

The first code (which has not used info ) does not break your code is the fact that you have a game element At this time, the tester is executed (which is full of documents), and not when the script gets the tag (which, as you notice, does not work).

No comments:

Post a Comment