REVISED: Sunday, March 3, 2013
I. JAVASCRIPT
A. Programming Tool
1. Statements
JavaScript is a scripting language that gives HTML designers a programming tool.
A JavaScript statement is a command to a browser.
It is normal to add a semicolon at the end of each executable statement.
Each statement is executed by the browser in the sequence they are written.
JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and end with a right curly bracket }. A block is used to group statements together in a function or in a condition, where a group of statements should be executed if a condition is met.
JavaScript is case sensitive.
JavaScripts can be put in the <body> and in the <head> sections of an HTML page.
2. Comments
Single line JavaScript comments start with //. Multi line comments start with /* and end with */.
3. Rules for Variables
You declare JavaScript variables with the var keyword.
Variable names are case sensitive; z and Z are two different variables.
Variable names must begin with a letter or the underscore character.
When you assign a text value to a variable, use quotes around the value.
A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. Local variables are destroyed when you exit the function. Variables declared outside a function become GLOBAL. Global variables are destroyed when you close the page. If you declare a variable, without using "var", the variable always becomes GLOBAL. If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables.
4. Assignment Operators
Given x=20 and y=10 then:
Assignment = x=y x=y x=10 y=10
Addition += x+=y x=x+y x=30 y=10
Subtraction -= x-=y x=x-y x=10 y=10
Multiplication *= x*=y x=x*y x=200 y=10
Division /= x/=y x=x/y x=2 y=10
Modulus %= x%=y x=x%y x=0 y=10
Increment ++ x=++y x=11 y=11
x=y++ x=10 y=11
Decrement -- x=--y x=9 y=9
x=y-- x=10 y=9
B. Uses
JavaScript can be used to:
1. React
React to events such as when a page has finished loading or when a user clicks on an HTML element.
2. Read and Write
Read and write HTML elements.
3. Validate
Validate form data before it is submitted to a server. This saves the server from extra processing.
4. Detect
Detect the visitor's browser.
5. Create
Create cookies and store and retrieve information on the visitor's computer.
C. Browser Support
Some Browsers do Not Support JavaScript.
Browsers that do not support JavaScript, will display JavaScript as page content.
To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript.
Just add an HTML comment tag <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, as shown in the first example below:
II. JAVASCRIPT EXAMPLES
A. Date Button
The following is an example of using JavaScript to create a button that displays the date:
<html>
<head>
<script type="text/javascript">
<!--
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
//-->
</script>
</head>
<body>
<h1>Please left click button for today's date:</h1>
<p id="demo">JavaScript Date Button.</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html>
EXAMPLE OUTPUT:
Sat Oct 22 2011 19:20:11 GMT-0400 (Eastern Daylight Time)
Elcric Otto Circle