JAVASCRIPT LESSON 1
Open up Notepad. Start by writing or copying this:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Save the file as hello_world.html or whatever name you prefer.
Now insert the script tags.
<html>
<head>
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
</body>
</html>
Notice that the script tags are after the title tags but between the head tags.
They should not go between the body tags (for now). More on this in chapter 3.
Now insert a empty function.
<html>
<head>
<title></title>
<script type="text/javascript">
function () {
}
</script>
</head>
<body>
</body>
</html>
A function gives instructions or tells what to do.
Now give the function a name. We'll call it Hello for now.
<html>
<head>
<title></title>
<script type="text/javascript">
function Hello () {
}
</script>
</head>
<body>
</body>
</html>
Now we'll insert an alert box that says "Hello there."
<html>
<head>
<title></title>
<script type="text/javascript">
function Hello () {
alert ("Hello there!")
}
</script>
</head>
<body>
</body>
</html>
Notice how the function is structured. Pay close attention to the following:
"" - quotation marks
() - parentheses
{} - curly brackets
Don't worry about these now. We'll get into them later.
The function won't work by itself. You need to insert a link that calls the
function.
<html>
<head>
<title></title>
<script type="text/javascript">
function Hello () {
alert ("Hello there!")
}
</script>
</head>
<body>
<a href="javascript:Hello()"> Hello there </A>
</body>
</html>
And you're done. Not TRY IT.
VERY IMPORTANT: JavaScript is CASE-SENSITIVE.
function Hello is not the same as function HELLO.
Case-sensitive errors can be very frustrating. Trust me. A capital letter makes a
world of difference in javascript.
|