JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
The Web Warrior Guide to Web Design Technologies
Computer Science 103 Chapter 4 Advanced JavaScript.
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
CST JavaScript Validating Form Data with JavaScript.
Internet and Web Application Development Revision.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
JavaScript 1. What is JavaScript? JavaScript allows web authors to create dynamic pages that react to user interaction. It is an Object-based because.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Topic: An Introduction to JavaScript - from Beginning JavaScript by Wilton (WROX)
Using Client-Side Scripts to Enhance Web Applications 1.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JAVASCRIPT A quick review. True False ■The DOM is a standardized way of referring to parts of a Web page. ■TRUE ■In the DOM, attributes have their own.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Introduction to JavaScript DOM Instructor: Sergey Goldman.
Tarik Booker CS 120 California State University, Los Angeles.
REEM ALMOTIRI Information Technology Department Majmaah University.
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Week 4: Introduction to Javascript
Introduction to JavaScript Events
Intro to JavaScript CS 1150 Spring 2017.
Web Development & Design Foundations with HTML5 7th Edition
JavaScript Functions.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript: Functions.
14 A Brief Look at JavaScript and jQuery.
DHTML Javascript Internet Technology.
Events Comp 205 Fall 2017.
DHTML Javascript Internet Technology.
Training & Development
Tutorial 10: Programming with javascript
Web Programming and Design
Web Programming and Design
Web Programming and Design
Presentation transcript:

JavaScript Part 6

Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate that by having only functions in our JavaScript and calling a specific function when an HTML page loads or when an event happens such as, clicking a button or submitting a form. This can be done in different ways:  In HTML: Using the attribute onload or onclick inside any HTML element. Example:  In JavaScript: Using window.addEventListener. When a certain event occurs (page loads or mouse clicks) a JavaScript functions starts running. Example: window.addEventListener(“load”, myfunction, false);

Calling JavaScript functions on an event <!-- function start() { document.writeln(" The function has started. "); f1(); } function f1() { document.writeln(" We are now in function f1. "); } // --> Output:

Calling JavaScript functions on an event <!-- function f1() { document.writeln(" You have pressed Button 1. "); } function f2() { document.writeln(" You have pressed Button 2. "); } // --> Button 1 Button 2

Calling JavaScript functions on an event <!-- function start() { document.writeln(" The function has started using window.addEventListener. "); f1(); } function f1() { document.writeln(" We are now in function f1. "); } window.addEventListener("load", start, false); // -->

document.getElementById() method The getElementById() method returns the element that has the ID attribute with the specified value. This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element on your document. Returns null if no elements with the specified ID exists. An ID should be unique within a page. However, if more than one element with the specified ID exists, the getElementById() method returns the first element. In JavaScript: var par1 = document.getElementById(“p1”); In the HTML body:

After getting the element by using document.getElementById() you can change the element’s content or attributes. You can also add an event listener to the element. // to change the content inside the HTML element par1.innerHTML = “New text of paragraph with id p1”; //to change the attributes values of the element par1.setAttribute(“style”,”color:red”); par1.style.color = “red”; //another way img1.src = “Kuwait.jpg”; //changing the image source for an img element with id = img1 var imagesrc = img1.src; //getting the value of the attribute as a string //add an event listener to an element par1.addEventListener(“click”, myfunction, false); document.getElementById() method

<!-- function f1() { var par1 = document.getElementById("p1"); par1.innerHTML = "You have pressed Button 1."; par1.setAttribute("style","color:blue"); } function f2() { var par1 = document.getElementById("p1"); par1.innerHTML = "You have pressed Button 2."; par1.style.color = "red"; } // --> Click any button and I will tell you which button you clicked Button 1 Button 2

document.getElementById() method

Processing form values in JavaScript

JavaScript code in a separate file The JavaScript code can be written in a separate file with the extension.js and put the file name in the src attribute of the script element. This easily allows you to use any JavaScript file available on the web in your web site.

Ch10: JavaScript Arrays Arrays are used to store multiple values in a single variable. There are a couple of ways to create and initialize an array. var arr = [1, 2, 3]; var cars = new Array(“Toyota”, “BMW”, “Honda”); Arrays can be created without being initialized. var arr = new Array(5); //array arr is created and memory is allocated for 5 elements var c = new Array(); //array c is created and memory will be dynamically allocated as elements // are added to the array A single value in an array can be accessed by its index number. The array index starts with a zero. document.writeln(arr[0]); //writes 1 in the HTML document arr[1] = 3; // arr values become 1,3,3 Unlike other programming languages, arrays in JavaScript can store different types of values. var person = [“John”, “Doe”, 46];

Array properties and methods The real strength of JavaScript arrays are the built-in array properties and methods: var x = cars.length; // The length property returns the number of elements in cars var y = cars.sort(); // The sort() method sort cars in alphabetical order The length property of an array returns the length of an array (the number of array elements). var fruits = ["Banana", "Orange", "Apple", "Mango"]; var l = fruits.length; // the length of fruits is 4 Elements can be added dynamically to the array increasing its size. The length property is used to add elements to the end of the array.

Adding elements to an array The length property provides an easy way to append new elements to an array without using the push() method. Try it <!-- var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits[fruits.length] = "Kiwi"; document.getElementById("demo").innerHTML = fruits; } // -->

Adding elements to an array

Multidimensional Arrays To identify a particular two-dimensional multidimensional array element Specify the two subscripts By convention, the first identifies the element’s row, and the second identifies the element’s column In general, an array with m rows and n columns is called an m-by-n array Two-dimensional array element accessed using an element name of the form a[ i ][ j ] a is the name of the array i and j are the subscripts that uniquely identify the row and column Multidimensional arrays are maintained as arrays of arrays var a = [[1,2,3], [4,5,6],[7,8,9]];

20 Fig | Two-dimensional array with three rows and four columns.