Lesson 2
Lesson 2
Lesson 2
• Defined
• DOM (Document Object Model)
• General Syntax
• Body vs. Head
• Variables
• Math & Logic
• Selection
• Functions & Events
• Loops
• Animation
• Getting Help
What is JavaScript?
1. JavaScript is NOT Java!
2. JavaScript was designed to add interactivity to
HTML pages
3. JavaScript is a scripting language
4. JavaScript is usually embedded directly into
HTML pages
5. JavaScript is an interpreted language (means
that scripts execute without preliminary
compilation)
6. JavaScript is free and open-source
What can a JavaScript do?
1. JavaScript gives HTML designers a
programming tool (very simple syntax)
2. JavaScript can put dynamic text into an HTML
page
3. JavaScript can react to events
4. JavaScript can read and write HTML elements
5. JavaScript can be used to validate data
(validate form data)
6. JavaScript can be used to detect the visitor's
browser
7. JavaScript can be used to create cookies
DOM (Document Object Model)
• In object-oriented programming we look at a
program as a collection of interacting objects.
– Objects have properties (facts about them)
• Length, width,
– Objects have functions (things they can do)
• write(), shoot()
• In JavaScript the document is considered to
be an object.
"dot" syntax
• Using Javascript we can access the properties
of a document and change a document using
special functions.
• When you see a dot (.) that implies ownership
• Examples:
document.title <!-- the title of the document -->
document.images[1] <!-- the second image in page -->
document.write("Hi!") <!-- function that writes text -->
First JavaScript
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
document.write("<h1>Hello World!</h1>");
//-->
/script>
</body>
</html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box called by onload event");
}
</script>
</head>
</html>
• You can also assign values to the variables when you declare
them:
var x=5;
var carname="Volvo"; // Text requires quotes
• If you assign values to variables that have not yet been declared,
the variables will automatically be declared.
x=5;
carname="Volvo";
• See 3_Var_PromptBox
Math && Logic
JavaScript supports the following operators
Mathematical:
+, -, *, /, %, ++, --
Logical:
&&, ||, !, <, >, etc...
var x=10;
var y=2;
var z=x/y;
Note: If you add a number and a string, the result will be a string!
Selection (the 'if' statement)
<script type="text/javascript">
// Comments are VERY VERY important
// If the time < 10, "Good morning" greeting.
// Otherwise you will get a "Good day" greeting.
alert("sometext");
confirm("sometext");
// returns "true" or "false")
prompt("sometext","defaultvalue");
//returns what user enters as answer
• See 6_PopUps
Changing Page Properties
• Remember the DOM of JavaScript allows you to
change the properties of a page… like the CSS
sheet.
<head>
<link rel="stylesheet" type="text/css" href="default.css" id="css_sheet"/>
Examples:
SimpleAnimation1.html
SimpleAnimation2.html
SimpleAnimation3.html
Getting Help
W3C JavaScript Tutorial:
http://www.w3schools.com/js/default.asp
JavaScript Animations:
http://www.schillmania.com/content/projects/javascript-
animation-1/
JavaScript Games:
http://www.devx.com/webdev/10MinuteSolution/27134