Javascript (NOT Java!) is a programming language that needs only a browser to run and a simple text editor (like notepad) to
write. Programmers like to call it a scripting language, maybe since it is simply interpreted by the browser (not compiled into
an .exe file) and cannot be used to produce software. Otherwise, it can be as object oriented as any high level language, it also has
many of their functionalities. However, because of security reasons, its output and input are limitted only to and from the browser
(unless it is used on the server as a server-side script).
In this example, we used the button tag as our element, onclick="" as an attribute, and alert() as a Javasript command.
So far we saw only attributes that modify the appearance of an element, but there are also attributes which, when used in
conjunction with Javascript, respond to actions based on the script code. The most commonly used are onclick="", oncdblclick="",
onmouseover="", onmouseout="". Their names are self-descriptive of their functions, for example, onmouseover() responds when the
mouse is placed ove the element. Now, let's break down our simple code.
onclick=""
1. This sends a message to a Javascript code when the user clicks on the element (here, the element is a button)
2. Place Javascript commands, separated by semi-colons(;), within the quotes.
3. You can use single-quotes instead of double quotes.
alert()
1. This is a Javascript command (or statement) that displays an alert box, nothing more -- just click the OK button of the alert box to close it.
2. Within the parenthesis place what ever message you want to display in single- or double-quotes.
3. Don't confuse the quotes!! If you use single-quotes for onclick, then use double-quotes for the alert, and vise-versa.
A variable is a means of storing data for later use. It is just a name, such as x, x1, y, myIncome, etc., that is created by
the programmer to hold data values. For example, the variable x may hold a numeric value of 100, a string value of
"Joe", or a boolean value of either true or false. Variable names are case-sensitive -- x an X are
completely different variables.
A constant is a variable whose value doesn't change within the program. A string constant is placed inside single-
or double quotes.
An operator is a symbol or string that performs a manipulation of certain data. Basic operators are addition,
subtraction, multiplication, division, and assignment. These operatotr in Javascript are, respectively, +, -, *, /, =.
The + operator is obviously used to add numbers, but it is also used to cocatenate (join) strings. For instance,
"They call me " + "Mr. Tibbs" will result in a single string "They call me Mr. Tibbs".
An expression manipulates variables and constants with help of operators, like x + x1.
The asignment operator ( = ) stores data into a variable. This is done by placing the variable
on the left of the equal sign and an expression to its right. For example, x = 25.6 + y;, is a statement that
adds 25.6 to the value stored in the variable y and stores the result in the variable x. A series of Javascript statements are
separated by semi-colons ( ; ). And a group of statements are placed within curly brackets ( {} )
Now, practice using the alert command to see if you can write correct Javascript expressions. Note that what actually goes inside the
parenthesis of alert is an expression. So, the rule is alert(expression)
Interactive Exercise
Below, in the 'Coding Area', there are a series of Javascript statements that assign values to variables and also an alert
statement. Change the expression inside the alert (or add more alerts) and click on the 'Show alert' button to see if you have
corectly written an expressions. If there is an error, a message will be displayed at the bottom. You czn copy and pate the
Exercise Hints to try them.
Coding Area
Exercise Hints
.
Try the following. If ERROR occurs, investigate the cause and correct it.
alert(s + s1 + "Joe")
alert("x times y " + s1 +" "+ x*y)
alert("x * y = " + xy)
alert("y/x = " y/x)
alert(x+y)
alert("x + y = " + x + y)
Alert(x+y)
alert("x + y = " + (x + y))
alert(x + y = (x + y))
alert('Tim Tibow ' + S1 + ' ' + S)
First off, we used five form elements, three text type inputs and two buttons, with names assigned to the form itself
as well as to the form elements that require a name. Now, in Javascript, the value of a form element is accessed by takig a path
to the form element as document.form1.Iqty.value. What this means is "within this document find a
form named form1, then inside that form find a form element named Iqty, and get or modify the value (content) this
form element."
So, what the script inside the onclick does is: divide what the user entered inside the 'Iqty' element by what he/she entered in
the 'Tqty' element, multiply by 100, concatenate with the string "%", and put the result in the 'Perc' element. The last statement,
'return false' is just to please some stupid browsers such as Firefox, and it is not part of the program we are dealing with here.
Note that we added a reset button just to show its application -- it clears all values. Also, we added 'readonly' attribute
to the 'Perc' element since no input should be entered there.
Javascript in conjunction with styles can be effectively used to make web pages dynamic and interactive. This technique is known
as DHTML and will be the our next subject.