O. Brainwave O. Brainwave - 1 month ago 6
Javascript Question

Function of Javascript in HTML

I'm sorry if this question is too trivial. Since I'm a beginner, it is difficult for me to understand some explanation. What is the meaning of the following statement?

"To keep the browser from executing a script when the page loads, you can put your script into a function."

Answer

I think the author meant that if you do this:

<script>
  alert('Hello');
</script>

The script will run when the page load. But you can wrap that in a function:

<script>
  function hello() {
    alert('Hello');
  }
</script>

So instead of popping "Hello" the script will register the function that can be called later from another place/script to display "Hello" on the screen.