Web-Based Information Course Content Systems • Introduction • Databases & WWW • Internet and WWW • SGML / XML Winter 2002 • Protocols • Managing servers • HTML and beyond • Search Engines CMPUT 499: JavaScript • Animation & WWW • Web Mining • Java Script • CORBA Dr. Osmar R. Zaïane • Dynamic Pages • Security Issues • Perl • Selected Topics • Java Applets • Projects University of Alberta Dr. Osmar R. Zaïane, 2001 1 Dr. Osmar R. Zaïane, 2001 2 2 Web-based Information Systems University of Alberta Web-based Information Systems University of Alberta Introduction to Variables I. JavaScript and the Details Objectives: Learn how JavaScript stores data, how a • A variable in Javascript has a type: document is structured in JavaScript, learn event-based programming with JavaScript. – number (integer or non integer) – String – Boolean • Variable identifiers and their types – Null • The notion of objets • JavaScript is not strongly typed. • Arrays Content • Control structures • Condition and selection • Iteration • Procedures and functions Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web-based Information Systems University of Alberta 3 Web-based Information Systems University of Alberta 4
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: – Change the type of a variable; The identifier must start with a letter or underscore ‘_’ and can 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 5 Dr. Osmar R. Zaïane, 2001 6 Web-based Information Systems University of Alberta Web-based Information Systems University of Alberta Variable Examples Variable Examples (con’t) <HTML> <BODY> <HEAD> <script language="JavaScript"> <TITLE> Mon premier JavaScript avec variables </TITLE> <!-- hide script <script language="JavaScript"> document.write("monAddition="+monAddition+"<BR>"); <!– hide script document.write("maConcatenation="+maConcatenation+"<BR>"); var monNombre=35; document.write("monErreur="+monErreur+"<BR>"); var maChaine="2000"; document.write("monReve="+monReve+"<BR>"); var monAutreChaine=“CMPUT499"; monErreur = monNombre * 3; var monAddition = monNombre+monNombre; document.write("monErreur="+monErreur+"<BR>"); var maConcatenation = maChaine + monAutreChaine; monNombre="Salut"; var monErreur = monNombre + monAutreChaine; document.write("monNombre="+monNombre+"<BR>"); var monCalcul = monNombre + maChaine; // end of hide --> monAddition=70 var monReve = monAutreChaine + maChaine; </script> maConcatenation=2000TICE // end of hide --> </BODY> monErreur=35 CMPUT499 </script> </HTML> monReve= CMPUT499 2000 </HEAD> monErreur=105 monNombre=Salut Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web-based Information Systems University of Alberta 7 Web-based Information Systems University of Alberta 8
I. JavaScript and the Details JavaScript & Concept of Objects Objectives: Learn how JavaScript stores data, how a document is structured in JavaScript, learn event-based programming with JavaScript. • JavaScript is not an object-oriented language. • JavaScript is an object-based language. • There are many pre-defined objects, but • Variable identifiers and their types programmers can define their own objects. • The notion of objets • An object has attributes (specific properties) as • Arrays well as methods (behaviour of objects). Content • Control structures • An attribute could be a value or recursively • Condition and selection • Iteration another object. • Procedures and functions Dr. Osmar R. Zaïane, 2001 9 Dr. Osmar R. Zaïane, 2001 10 Web-based Information Systems University of Alberta Web-based Information Systems University of Alberta What are the Objects, What A Book is an Object are their Properties? Title 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 Dr. Osmar R. Zaïane, 2001 Web-based Information Systems University of Alberta 11 Web-based Information Systems University of Alberta 12
Access Object Properties Access Object Methods myObject.oneProperty myObject.oneMethod(parameters) Object Name . Attributre Name Object Name . Method Name ( parameters ) If the attribute is also an object, to access the property of the If there are no parameters: attribute’s attribute: myObject.oneMethod() myObject.oneProperty.aPropertyOfProperty Ex: Book.Editor.Address Ex: document.write(“Hello!”) document.MyForm.Name.value Dr. Osmar R. Zaïane, 2001 13 Dr. Osmar R. Zaïane, 2001 14 Web-based Information Systems University of Alberta Web-based Information Systems University of Alberta Object Date Predefined Object Classes • The object Date needs to be instantiated • There are many intrinsic pre-defined objects in JavaScript: with the keyword new . var today= new date(); –Date –Navigator • The class Date doesn’t have properties but –String –History the following methods: –Math –Location –Window –Form •getDate() •getYear() •getDay() •setDate() –Document etc… •getHours() •setHours() etc… •getMinutes() •setMinutes() • These objects have their pre-defined attributes and •getMonth() •setMonth() methods. •getSeconds() •getSeconds() •getTime(); •setTime(); •getTimezoneOffset() •setYear() Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web-based Information Systems University of Alberta 15 Web-based Information Systems University of Alberta 16
Recommend
More recommend