Similar to CSX JavaScript functions are most commonly placed in between the open and close had tags.
The reason for this is because the head portion of an HMO page gets loaded before any other part of
the HMO document so if you were to put your javascript code right before the closing body tag any javascript
call is made to functions by your page as it loads will result in an error.
Let's take a look at an example of a javascript function located in our head tag and called upon in
our body.
We won't get too much into the HDMI El here because by now most of this HCM El code should look familiar
but this javascript code will calculate your age in dog years.
A year of a person's life is equivalent to seven years of a dog's life.
Let's preview this file before we take a look at the code
as you can see we input our age in the first input field.
I'll put one just for demonstration once we press calculate the javascript function is called it reads
the value in the first input and multiplies it by 7.
It places the result in the second input
in the code we can see that the script for the javascript function is located in the head tag the function
itself is called C years this function takes the value of the form element my age and multiplies it
by 7.
The result is then displayed in the form element dog age the entire function is activated when the user
clicks the calculate button.
This is when the javascript function is called we can see and on click event attached to the button
we'll explore different events later in this course.