0054 Course Training Tutorial Js
0054 Course Training Tutorial Js
Web-based Applications
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 3 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 4
Content Introduction to Variables
I. JavaScript and the Details
• Variable identifiers and their types
• The notion of objets • A variable in Javascript has a type:
• Arrays
– number (integer or non integer)
• Control structures
• Condition and selection – String
• Iteration – Boolean
• Procedures and functions – Null
II. Event-Based Programming with JavaScript
• What is an event? • JavaScript is not strongly typed.
• What are the recognized events?
• Capturing events.
III. Practical Examples
• Data entry validation within a form;
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 5 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 6
Declaring Variables
Type Conversion on the fly
The first time a variable is used it must be declared with the
keyword ‘var’.
• Because JavaScript is not strongly typed, it
var identifier = value; is possible to:
The identifier must start with a letter or underscore ‘_’ and can – Change the type of a variable;
have as many characters as necessary (letters, digits, – Do operations on variables of different types.
underscore).
– The major type, or default type, is string.
Javascript is sensitive to capital letters.
myvariable is different from MyVariable and x ≠ X
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 7 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 8
Variable Examples Variable Examples (con’t)
<HTML> <BODY>
<HEAD> <script language="JavaScript">
<TITLE>My First Java Script with variables</TITLE> <!-- hide script
<script language="JavaScript"> document.write("myAddition="+myAddition+"<BR>");
<!– hide script document.write("myConcatenation="+myConcatenation+"<BR>");
var myNumber=35; document.write("myError="+myError+"<BR>");
var myString="2004"; document.write("myDream="+myDream+"<BR>");
var myOtherString=“CMPUT410"; myError = myNumber * 3;
var myAddition = myNumber+myNumber; document.write("myError="+myError+"<BR>");
var myConcatenation = myStyring + myOtherString; myNumber=“Bye!";
var myError = myNumber + myOtherString; document.write("myNumber="+myNumber+"<BR>");
var myCalculation = myNumber + myString; // end of hide --> myAddition=70
var myDream = myOtherString + myString; </script>
myConcatenation=2004CMPUT410
// end of hide --> </BODY>
</script> </HTML> myError=35CMPUT410
</HEAD> myDream= CMPUT4102004
myError=105
myNumber=Bye!
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 9 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 10
Content
I. JavaScript and the Details
• Variable identifiers and their types JavaScript & Concept of Objects
• The notion of objets
• Arrays
• Control structures • JavaScript is not an object-oriented language.
• Condition and selection • JavaScript is an object-based language.
• Iteration • There are many pre-defined objects, but
• Procedures and functions programmers can define their own objects.
II. Event-Based Programming with JavaScript
• What is an event? • An object has attributes (specific properties) as
• What are the recognized events? well as methods (behaviour of objects).
• Capturing events. • An attribute could be a value or recursively
III. Practical Examples another object.
• Data entry validation within a form;
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 11 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 12
A Book is an Object What are the Objects, What
Title
are their Properties?
Authors
Editors
Number of pages
Price
Set of Chapters
Set of figures and images
etc.
?
Each book has the same
attributes with different
values
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 13 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 14
document.MyForm.Name.value
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 15 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 16
Predefined Object Classes Object Date
• There are many intrinsic pre-defined objects in • The object Date needs to be instantiated
JavaScript: with the keyword new.
var today= new date();
–Date –Navigator
–String –History • The class Date doesn’t have properties but
–Math –Location the following methods:
–Window –Form •getDate() •getYear()
–Document etc… •getDay() •setDate()
•getHours() •setHours()
•getMinutes() •setMinutes()
etc…
• These objects have their pre-defined attributes and •getMonth() •setMonth()
methods. •getSeconds() •getSeconds()
•getTime(); •setTime();
•getTimezoneOffset() •setYear()
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 17 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 18
Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 19 Dr. Osmar R. Zaïane, 2001-2006 Web-based Information Systems University of Alberta 20
Click here to download full PDF material