Javascript 1
To make things happen on a web page we need to write some javascript code.
Here is the code that said 'hello' when you first loaded this page:
function hello(){
alert("hello");
}
hello();
Understanding Javascript
The word function creates a block of code we can use whenever we wish.
The name hello is a name I chose, because the code says 'hello'! 😄
Next we have two round brackets: ( and ).
Inside the curly brackets: { and }, we write the code that tells the computer what to do.
In this example we use the method alert which puts a message box on the screen.
Inside alert's round brackets: ( and ), we write our message.
We put our message in quote marks like this "Write your message in quotes!"
At the end of the line we put a semi-colon: ;
The code won't do anything at all until we call it.
We do that by writing:
- the name of the function: hello,
- two round brackets: ()
- and a semi-colon: ;
When the page is loaded the browser gets to the code hello(); and runs it.
So we see the message on the screen.
Try it out yourself!
On your web page you will need to write <script>, then write your javascript and then write </script>
Like this:
<script>
Write your javascript here
</script>
Your code will look a lot like mine. There are some things you can change:
- The name of the function
- The message in alert's brackets.
There are some things that you can't change: javascript won't work without them!
- Your code must be in between <script> and </script>
- Your code must start with the word function
- After the name you choose there must be round brackets: ()
- Then there must be curly brackets: { .... }
- Your code must be inside the curly brackets
- It must call 'alert()' just like I do
- With a message in quotes (".....")
- There must be a semi-colon at the end of the alert's brackets
- You must call your function by typing its name, two round brackets and a semi-colon.
Please ask for help if you get stuck.
Good luck! 😄