JavaScript Programming an Introduction
Prepared ByP .D. Krolak and M.S. Krolak
Based on material from
JavaScript Unleashed (2nd Ed)
JavaScript Objects• Objects hold data
•Objects have Properties that can be set and have values
•Objects can have Methods
JavaScript
• JavaScript is a Programming Language, although not a complete one.
• JavaScript was developed by Netscape and is becoming an international standard
• JavaScript is sent with the HTML document and is interpreted at the time it is loaded on the browser
• JavaScript adds interaction
JavaScript Objects
• JS is not true Object Oriented Programming (OOP) but object like
• What are the JS’s objects
• What is the JS object Hierarchy
• Built-In Language Objects
JS is not true OOP but object like.
• JS objects do have properties and methods like and object orientated language.
• JS objects do not support inheritance.
• JS object model is a container model not a class model.
• Container objects contain other containers.
What are the JS’s objects
• JavaScript objects fall into classes– Navigator Objects that mostly correspond to
HTML tags– Built-in Language Objects
JavaScript Objects & Corresponding HTML Tags
window N/A
frame <frame>
document <body>
form <form>
button <Input Type=”button”>
checkbox <Input Type=”checkbox”>
fileUpload <Input Type=”file”>
JS Navigator object
• Contains information about the browser.
• Properties can not be set by Javascript.
JS History Object
• The History object records the documents that were displayed in order of their presentation – most recent descending to oldest.
JS History Properties & Methods
Methods Description
Back () Go back n pages – no argument go to last page
Forward() Go forward n pages if possible,
Go() Go N pages where n >0 for forward and n<=0 for back.
JS History Example
Create a back to last page link
<a HREF=“javascript:history.back();”>
Go to Last Page </a>
JS Document Object
• Contains all the object that are contained in the document. The objects that are contained correspond to the HTML tags found on the web page.
JS Location Object
• Location Object contains the information about the source of the document, i.e. the URL, and related information.
Built-In Language Objects
• Built Objects do not appear in the Document but in the code.
• Built-in Objects include:– Array– Date– Math– String
JavaScript Properties
• Properties in JS resemble data attributes of an object
• Properties explain the characteristics and identity of the given object
• Properties can represent the state of the object
• Properties could represent the role that the object plays at a given time
JavaScript Events & Event Handlers
• A javaScript event is controlled by browser based on action normally initiated by the viewer.
• Each tag has an associated set of event handlers that are triggered by a corresponding event. When the event handler is triggered, a javaScript is preformed.
JavaScript Methods
• Methods are functions that provide services for the object
1. Set the value of a property
2. Get the value of a property
3. Iterate on the object’s properties
4. Constructor for the object.
Embedding JS in HTML
• The <script> tag• Accommodating the Non JS supporting browsers
by using a comment to surround the content of the script within the container.
• Viewing the JS code• Executing the scripts• loading a page• HTML enhancements
The <script> tag
• <script language=“JavaScript1.x”> where x can be version of JavaScript used, i.e 0,1,2,3. If the browser does not support the version the tag and its content is ignored.
• language can also be set to vb script, php, etc.• <Script > </Script> form the container• The script may be placed anywhere in the document but
must exit before needed.• Most scripts are placed in the head and are thus loaded
before they are needed for the display & interaction.• Scripts that contain document.write() must be in the
body.
The <script> tag continued
• JavaScript libraries may be stored external to the HTML document.
• <script Language=javascript1.2 src=“Library’s_URL”>
• Library file extension must be “.js”
Accommodating the Non JS supporting browsers
• The contents of the script container are commented so non JS browsers will ignore the contents.
• <Script ><! -- // the comment is used by the non-JS Your javaScript code here--></Script>
Viewing the JS code
<Html>
<Head>
</Head>
<Body>
<script Language=“javascript1.2”>
document.write(“Hello World”);
</Script>
</Body>
</Html>
Executing the scripts
• The scripts are not executed in any necessary order.
• They are executed as the flow of events dictates.
Versions of JS
Version of Javascript Browser Support
Javascript 1.1 •Netscape 2.
Javascript 1.2 Netscape
Javascript 1.3 Netscape 4.
JS Operators
• Assignment Operators
• Arithmetic Operators
• Comparison Operators
• String Operators
• Conditional Operators
• Boolean Operators
• The type of operators
Arithmetic Assignment
Assignment Description
x += y x= x+y addition
x -=y x= x-y subtraction
x *= y x= x*y
x /= y x= x/y
x %= y x= x%y x mod (y)
Arithmetic Operators
Operator Description
% or Mod 4%3 returns 1
++ Increment ++x sets x= x+1 and returns x+1 but x++ sets x=x+1 and returns x
-- Decrement --x sets x= x-1 and returns x-1 but x-- sets x=x-1 and returns x
- (uninary negation) Sets x = -x ,I.e reverses sign
Comparison Operators
Operator Description
== Equal
!= Not Equal
> Greater than
>= Greater than or Equal
< Less than
<= Less than or Equal
String
• String operators are the same as comparison, using a sort sequence
String a> b, or a>=b etc.
String has two forms of concatenate
1. a+b
2. a +=b
JS Operators (continued)
• Function operators
• Data structure operators
• Bit wise operators
• Operator precedence
Statements
• Statements define the flow of the script.• JS statements require a “;” at the end if
there are more than one on a line. • A block of statement that are executed in
order is enclosed by curly brackets {}.• Flow is normally linear but may be altered
by conditional, looping, or similar statements.
Comments
• Comments are not really part of the program statement but are provided for the programmer and others as notes and reminders of what is happening in the JS.
Statement; //single line comment
/* this the way to
create multi line comments */
JS Control Structures and Looping
• Conditional statements
• Looping statements
• Labeled
• with
• switch
Looping statements
ForThe traditional for loop loops until the test condition
is false. The initial statement is executed once. The test is applied and if true, the Block of Statements are executed. The counter is incremented and test applied. Repeat looping.
for (initial_statement; test_condition; increment)
{Block of Statements;}
Looping statements
For … inOn each iteration get one property of the
object. If the object has no properties the loop is not executed.
for (property in object)
{Block of Statements;}
Looping statements
do …. while
Repeat the block statement until the condition is false.
(Note it goes thru loop at least once)
do
{ Block of Statements; }
while (condition)
Looping statements
While
While executes as long as condition is true. Will not execute the first time if false.
While (condition)
{ Block of Statements;}
Break & Continue Statements
• Break – Drop out of loop and go to statement following loop.
• Continue – Drop out of the loop block of statements and go to the loop control. Looping continues until control is false.
Labeled
• Label statement – allows the break and continue statement to transfer to this label.
Label: statement;
switch
Switch is the JavaScript case statement.Switch (expression){
case label-1:{Block of Statements;}break….case label-n:{Block of Statements;}breakdefault:{Block of Statements;}
}
Object and Dot Notation
• The object is described from the container that holds the container that holds .. the container. Read from left to right and the dot as contains.
Example the form object from the diagram
Window.document.form
Object Dot Notation
• A property is:
Object.property where dot reads is_a
• A method is:
Object.method() where dot reads is_a
Object Dot Notation
• Since a window contains everything, it is called self.
• In the Dot notation we can drop the window because it is contained by nothing and contains everything.
JS Object Constructor
• A method is a constuctor if it has the same name as the object an provides a template for creating an instance of the object.
Function object(parm1, … ){
This.property1 = parm1 ;..This.method1= function1..}
Debugging JavaScript
• First sign of trouble will appear as error message in the status window (lower left bottom of window).
When a error does appear then type:
Javascript: in the location window (where the document URL goes). Note the last character “:” is required. A series of error messages that will attempt to locate the error will appear in the dialog window.
When creating JavaScript turn Javascript Console automatically 1. Can not be done through preference option But it can
done.
2. Go to the pref.js in the Netscape directory.
3. Open in a text editor.
4. Last line add:user_pref(“javascript.console.open_on_error”,
true);
5. Save file and Restart Netscape.
Console should pop up when a javascript error occurs.
Netscape JS Debugger
• Download the JS debugger from their web site:
http://developer.netscape.com/software/jsdebug.html
Top Related