Summary

This document likely contains a tutorial or guide covering the fundamentals of JavaScript programming. It probably covers topics such as variables, data types, operators, control flow, and functions.

Full Transcript

Introduction to JavaScript Table of Contents What is DHTML? DHTML Technologies ï‚­ XHTML, CSS, JavaScript, DOM 3 Table of Contents (2) Introduction to JavaScript ï‚­ Wh...

Introduction to JavaScript Table of Contents What is DHTML? DHTML Technologies  XHTML, CSS, JavaScript, DOM 3 Table of Contents (2) Introduction to JavaScript  What is JavaScript  Implementing JavaScript into Web pages  In part  In part  In external.js file 4 Table of Contents (3) JavaScript Syntax  JavaScript operators  JavaScript Data Types  JavaScript Pop-up boxes  alert, confirm and prompt  Conditional and switch statements, loops and functions Document Object Model Debugging in JavaScript 5 DHTML Dynamic Behavior at the Client Side What is DHTML? Dynamic HTML (DHTML)  Makes possible a Web page to react and change in response to the user’s actions DHTML = HTML + CSS + JavaScript DHTML XHTML CSS JavaScript DOM 7 DTHML = HTML + CSS + JavaScript HTML defines Web sites content through semantic tags (headings, paragraphs, lists, …) CSSdefines 'rules' or 'styles' for presenting every aspect of an HTML document  Font (family, size, color, weight, etc.)  Background (color, image, position, repeat)  Position and layout (of any object on the page) JavaScript defines dynamic behavior  Programming logic for interaction with the user, to handle events, etc. 8 JavaScript Dynamic Behavior in a Web Page JavaScript JavaScript is a front-end scripting language developed by Netscape for dynamic content  Lightweight, but with limited capabilities  Can be used as object-oriented language Client-side technology  Embedded in your HTML page  Interpreted by the Web browser Simple and flexible Powerful to manipulate the DOM (Document Object Model) 10 JavaScript Advantages JavaScript allows interactivity such as:  Implementing form validation  React to user actions, e.g. handle keys  Changing an image on moving mouse over it  Sections of a page appearing and disappearing  Content loading and changing dynamically  Performing complex calculations  Custom HTML controls, e.g. scrollable table  Implementing AJAX functionality 11 What Can JavaScript Do? Can handle events Can read and write HTML elements and modify the DOM tree Can validate form data Can access / modify browser cookies Can detect the user’s browser and OS Can be used as object-oriented language Can handle exceptions Can perform asynchronous server calls (AJAX) 12 What is AJAX? AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and Java Script. Ajax uses XHTML for content, CSS for presentation, along with Document Object Model and JavaScript for dynamic content display. 13 What is AJAX? Conventional web applications transmit information to and from the sever using synchronous requests. It means you fill out a form, hit submit, and get directed to a new page with new information from the server. With AJAX, when you hit submit, JavaScript will make a request to the server, interpret the results, and update the current screen. In the purest sense, the user would never know that anything was even transmitted to the server. XML is commonly used as the format for receiving server data, although any format, including plain text, can be used. 14 The First Script first-script.html alert('Hello JavaScript!'); 15 Another Small Example small-example.html document.write('JavaScript rulez!'); 16 Using JavaScript Code The JavaScript code can be placed in:  tag in the head  tag in the body – not recommended  External files, linked via tag the head  Files usually have.js extension  Highly recommended  The.js files get cached by the browser 17 JavaScript – When is Executed? JavaScriptcode is executed during the page loading or when the browser fires an event  All statements are executed at page loading  Some statements just define functions that can be called later Function calls or code can be attached as "event handlers" via tag attributes  Executed when the event is fired by the browser 18 Calling a JavaScript Function from Event Handler – Example image-onclick.html function test (message) { alert(message); } 19 Using External Script Files Using external script files: external-JavaScript.html The tag is always empty. External JavaScript file: function sample() { alert('Hello from sample.js!') } sample.js 20 The JavaScript Syntax JavaScript Syntax The JavaScript syntax is similar to C# and Java  Operators (+, *, =, !=, &&, ++, …)  Variables (typeless)  Conditional statements (if, else)  Loops (for, while)  Arrays (my_array[]) and associative arrays (my_array['abc'])  Functions (can return value)  Function variables (like the C# delegates) 22 Data Types JavaScript data types:  Numbers (integer, floating-point)  Boolean (true / false) String type – string of characters var myName = "You can use both single or double quotes for strings"; Arrays var my_array = [1, 5.3, "aaa"]; Associative arrays (hash tables) var my_hash = {a:2, b:3, c:"text"}; 23 Everything is Object Every variable can be considered as object  For example strings and arrays have member functions: objects.html var test = "some string"; alert(test); // shows letter 'r' alert(test.charAt(5)); // shows letter 's' alert("test".charAt(1)); //shows letter 'e' alert("test".substring(1,3)); //shows 'es' var arr = [1,3,4]; alert (arr.length); // shows 3 arr.push(7); // appends 7 to end of array alert (arr); // shows 7 24 String Operations The + operator joins strings string1 = "fat "; string2 = "cats"; alert(string1 + string2); // fat cats What is "9" + 9? alert("9" + 9); // 99 Converting string to number: alert(parseInt("9") + 9); // 18 25 Arrays Operations and Properties Declaring new empty array: var arr = new Array(); Declaring an array holding few elements: var arr = [1, 2, 3, 4, 5]; Appending an element / getting the last element: arr.push(3); var element = arr.pop(); Reading the number of elements (array length): arr.length; Finding element's index in the array: arr.indexOf(1); 26 Standard Popup Boxes Alert box with text and [OK] button  Just a message shown in a dialog box: alert("Some text here"); Confirmation box  Contains text, [OK] button and [Cancel] button: confirm("Are you sure?"); Prompt box  Contains text, input field with default value: prompt ("enter amount", 10); 27 Sum of Numbers – Example sum-of-numbers.html Sum of Numbers function calcSum() { value1 = parseInt(document.mainForm.textBox1.value); value2 = parseInt(document.mainForm.textBox2.value); sum = value1 + value2; document.mainForm.textBoxSum.value = sum; } 28 Sum of Numbers – Example (2) sum-of-numbers.html (cont.)