1 / 23

Learning Outcome

EEE3112 Introduction to Multimedia Application & Technology Chapter 4: Web Development – Part 2: JavaScript by Muhazam Mustapha, October 2012. Learning Outcome. By the end of this chapter, students are expected to be ready to demonstrate the required second part skills for CO4:

leland
Download Presentation

Learning Outcome

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEE3112Introduction to Multimedia Application & TechnologyChapter 4: Web Development– Part 2: JavaScript by Muhazam Mustapha, October 2012

  2. Learning Outcome • By the end of this chapter, students are expected to be ready to demonstrate the required second part skills for CO4: • Writing simple JavaScript code

  3. Chapter Content • Syntax & Structure • JavaScript in HTML • Event Handling

  4. Syntax & Structure

  5. JavaScript In MVC design pattern, JavaScript plays the role as Control JavaScript Resembles C programming language Embeddable into HTML text The most popular language for automating HTML – second one is Microsoft’s VBScript A very light weight OBJECT-ORIENTED language and easy to learn Despite the name, JavaScript has nothing to do with Oracles / Sun Microsystem’s Java programming language

  6. JavaScript The source code of JavaScript is embedded into HTML, and the translation is done by the browser (not by the server that transferred it) For this reason, JavaScript programming is called client-side programming – as opposed to server-side programming that is translated/compiled at the server JavaScript was initiated by Netscape (no more in business) but standardized by ECMA International as ECMAScript

  7. <script> Tag Can be inserted into HTML either in <head> or <body> tags as <script> tag: <script language="JavaScript">...javascript code...</script> It can also be inserted as link / reference to an external file: <script language="JavaScript" src="myjs.js" />

  8. Language Structure Variable declaration: Optional Syntax: var variable_name; Only as generic type var – no specific type like integer, float, etc Semicolon (;) at the end of each statement is optional but recommended to be put for safety Most of other structures follow the syntax of C programming language

  9. Language Structure for and while loops: Syntax: for (initial; condition; increment) {statement; } while (condition) {statement; } break and continue work the same way as in C language

  10. Language Structure if-else structure: Syntax: if (condition) {statement; }else if (condition) {statement; }else {statement; } Follows the same behavior as C language

  11. Language Structure switch-case structure: Syntax: switch (selector)case value1: statement; break;case value2: statement; break;...default: statement; break; Follows the same behavior as C language, but selector values can be non-integer (like string)

  12. Object Structure Objects in OOP are entities that can have properties and behavior As an OOP, JavaScript can declare object: Syntax: var myObj = new Object(); Properties and behaviors can be accessed through dot (.) operator Syntax: myObj.prop1 = 235; For our scope we won’t be creating so much of our own object, but will only use predefined objects

  13. Array Array is declared as follows: Syntax: var myArr = new Array(); Then it can accessed using index as follows: Syntax: myArr[0] = 10;myArr[1] = 20;... Unlike C, JavaScript array has ‘associative’ index which may not be integer – it can also be string: myArr["last"] = "Last";

  14. Functions Functions are declared as follows: Syntax: function myFunc(parameters){...} To return value to the caller, use return keyword

  15. String Manipulation String is an object and can be declared like other variables Getting string length: myStr.length Chaining string: just by adding (+) longStr = shortStr1+shortStr2; Learn more from: http://www.w3schools.com/jsref/jsref_obj_string.asp

  16. Conversion to & from String To string: nonStr.ToString(); To integer: Decimal integer: parseInt(aStr, 10) Hexadecimal integer: parseInt(aStr, 16) Octal integer: parseInt(aStr, 8) To float: parseFloat(aStr)

  17. Mathematical Calculation For our class you are to learn on your own how to use JavaScript Math object from: http://www.w3schools.com/js/js_obj_math.asp http://www.w3schools.com/jsref/jsref_obj_math.asp Examples: Square root: Math.sqrt(value) π (Pi): Math.PI Natural Log: Math.log(value) etc

  18. JavaScript in HTML

  19. JavaScript Embedding Example:...other HTML...<script>var a = 5;alert(a+7);</script>...other HTML... Function alert() is to pop-up a dialog to show some values that may include debugging values

  20. Document Object Model DOM is the hierarchy of objects that is used by the browser to keep information of a webpage Like information about tables, rows, images, etc To access to a particular object in a web page we can use DOM However DOM is very complicated and hard to learn, and they are different among different browsers

  21. Referencing an HTML Object The best way to access objects in web page is by a short-cut function access: document.getElementById("idName"); Example: <a id="sample" href="target">A Link</a>...<script language="JavaScript">var aLink = document.getElementById("sample");</script>

  22. Accessing an HTML Object Once the object / element is referenced, we can use methods (function) setAttribute and getAttribute to read from and write to attributes: getAttribute("attrName"); setAttribute("attrName", "newValue"); The content of the tag can be accessed using innerHTML function

  23. Event Handling

More Related