Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't call function from body onload (uncaught reference error: start is not defined) javascript

I have a body onload calling a function in javascript. I Have tried many things, but the console just prints to the error log: uncaught reference error: start is not defined. I think it might be a malfunction, please notify me if it works for you. My code is as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Monster Invasion</title>
    <script type="javascript">
      var hur;
      var monsters = 10;
      var objective = false;
      var health = 100;
      var damage = 30;
      var sheild = false;
      var ea = 1;
      function start() {
        setTimeout(hurt,4000)
      }
      function hurt() {
        var newhelth = health - damage;
        
        health = newhelth;
        document.getElementById("healtw").innerHTML = health;
        start();
      }
      
      function kill() {
        if(monsters > 0) {
        monsters--;
        document.getElementById("monster1").src="dead.jpg"
        
        setTimeout(next,2000)
        }else {
          objective = true;
          document.location="endoflevel.html";
        }
      
      }
      function next() {
        document.getElementById("monster1").src="monster.jpg"
        
      }
      }
    </script>
  </head>
  <body onload="start()">
    <p id="healtw"></p>
    <embed src="guide_first_level.mp3" type="audio/mp3" hidden="true" autostart="true">
    <a id="st" onclick="kill()"><img id="monster1" src="monster.jpg"></a>
    <p id="ada"></p>
    <a href="sheild.html">Activate sheild</a>
  </body>
</html>
like image 223
Tae Hagen Avatar asked Jul 14 '15 02:07

Tae Hagen


People also ask

How do I fix reference error in JavaScript?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.

Why is JavaScript saying my function is not defined?

Function is not defined | JavaScript The reason for getting the error is very simple. The error occurs if you try to make a function call that you haven't defined. In the above code example, we did exactly the same thing, we called a function named sum(), which is not defined.

How do you fix function is not defined?

To clear a text input, assign blank string to 'value' attribute (not innerHTML attribute) document . getElementById("text"). value = ""; And don't call myfunction in JS tab If you use a <form> then an input with 'type' attribute "reset" will do the same thing.

Why function is not defined?

Conclusion # The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.


1 Answers

your script type is wrong.

try like this

<script type="text/javascript">
</script>

However, you don't need to mention script type. just use a plain script tag.

like this

<script>
</script>

One more thing to add that before the end of the script tag, you gave an extra }.

just remove it

function next() {
   document.getElementById("monster1").src="monster.jpg"
}
} // <-- just remove this bracket
</script>
like image 150
Anik Islam Abhi Avatar answered Oct 05 '22 21:10

Anik Islam Abhi