COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
The Web Warrior Guide to Web Design Technologies
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Tutorial 10 Programming with JavaScript
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
JavaScript, Fourth Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Introduction to JavaScript for Python Programmers
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
CST JavaScript Validating Form Data with JavaScript.
Debugging JavaScript CS346. IE Javascript Debugging Aids From IE6 on default: no debugging aid for Javascript Change setting: – Tools > Internet Options.
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
Programming with JavaScript (Chapter 10). XP Various things Midterm grades: Friday Winter Career Fair – Thursday, April 28, 2011 (11 am to 3 pm). – MAC.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Dynamic Web Authoring Week3 – Javascript Basic COM311H Zheng, School of C&M, UUJ1.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Dynamic Web Authoring JavaScript – Array and function (2) COM311H Zheng, School of C&M, UUJ1.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Javascript. What is JavaScript? Scripting (interpreted) language designed for the web Beware: JavaScript is case sensitive.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
1 Server versus Client-Side Programming Server-SideClient-Side.
1 JavaScript in Context. Server-Side Programming.
Dynamic Web Authoring JavaScript – Looping statements COM311H Zheng, School of C&M, UUJ1.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript, Sixth Edition
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Dynamic Web Authoring form validation (1) COM311H Zheng, School of C&M, UUJ1.
Tutorial 10 Programming with JavaScript
Web Systems Development (CSC-215)
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10 Programming with JavaScript
JavaScript: Introduction to Scripting
Web Programming and Design
Presentation transcript:

COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Teaching Plan Feedback on week3 practicals Array Function Method COM311 H Zheng, School of C&M, UUJ 2

Feedback on practicals Errors caused when copy & paste codes from word document – try to avoid Error – imaging loading Variable name: “function” – reserved word How to check the results window.document.write ( ) Use comments Submission: Only submit a correct live link once, no file upload at this stage Feedback – in class, and lab COM311 H Zheng, School of C&M, UUJ 3

COM311 H Zheng, School of C&M, UUJ 4 Array The array is a special type of variable Arrays – a collection of data Each element of an array is a variable a variable is a container to hold a value an array is a container to hold multiple values Javascript Array is an array object

Array example strawberry orange apple watermelon fruit1 fruit2 fruit3 fruit4 fruit[0] fruit[1] fruit[2] fruit[3] COM311 H Zheng, School of C&M, UUJ 5 Array identifier: fruit elements: fruit[0], fruit[1], fruit[2],fruit[3] length of the array: 4

6 Array element fruit[ 0 ] Identifier Square bracket Index

7 fruit = new Array( 4 ) identifier of the new instance of Array, or name of the Array The ‘new’ operator creates an instance This is the parent object of the new instance Length of the new fruit Array Pair of parentheses ‘assignment’ operator

Define an array The numbering of an array always begins at 0 var array_identifier = new Array(); var array_identifier = new Array(length); var array_identifier = new Array (element1, element2, element3,…elementn) COM311 H Zheng, School of C&M, UUJ 8

Array examples - 1 var rack = [ ]; rack[0] = “first”; rack[1] = “Second”; or: var rack=[ “First”, “Second”] COM311 H Zheng, School of C&M, UUJ 9 indexValue of element 0“First” 1“Second”

Array examples - 2 var numberArray = [1, 2, 3, 5, 8, 13, 21, 34]; var stringArray = ["Veni", "Vidi", "Vici"]; var mixedArray = [235, "Parramatta", "Road"]; Exercise: Define 4 Ulster campus using an Array. COM311 H Zheng, School of C&M, UUJ 10

Array of arrays var subArray1 = ["Paris", "Lyon", "Nice"]; var subArray2 = ["Amsterdam", "Eindhoven", "Utrecht"]; var subArray3 = ["Madrid", "Barcelona", "Seville"]; var superArray = [subArray1, subArray2, subArray3]; var city = superArray[0][2]; // get the third [2] element of the first [0] array, “Nice” COM311 H Zheng, School of C&M, UUJ 11

Array property length var myArray = [“first”,”second”,”third”]; var total = myArray.length; alert (total); //display in an pop-up alert window COM311 H Zheng, School of C&M, UUJ 12

COM311 H Zheng, School of C&M, UUJ 13 Function Function: a set of statements for a designed task Define a function: function function_name(parameter1, parameter2,…) { JavaScript Statements } List of arguments taken, can be empty, when called, need to have same length and order Valid identifier name

Functions (contd.) Defining functions – All variables declared in function are called local Do not exist outside current function – Parameters Also local variables – Promotes reusability Keep short Name clearly COM311 H Zheng, School of C&M, UUJ 14

Functions (contd.) Returning control - return statement, syntax: return expression ; examples: return true; return false; return (x+y); return; COM311 H Zheng, School of C&M, UUJ 15

COM311 H Zheng, School of C&M, UUJ 16 Function example: function doSomething( ){ var theVisitor = document.myform.visitor.value; Window.alert(“Is this OK,” + theVisitor + “?”); } function twoAdd(a, b){ var theResult = a+b; return theResult; } Q: how to call the function?

COM311 H Zheng, School of C&M, UUJ 17 Method Methods - Actions that are performed with or to an object can be used to: Open a new browser window Write text to the current XHTML document Navigate the browser history Display a dialog box Syntax: object name. method name (parameters)

COM311 H Zheng, School of C&M, UUJ 18 Method (contd.) Two useful output methods: document.write(“Greetings JavaScript Students”); window.alert (“You got 5 marks!); //BOM Note: you can use // to add comments behind a statement, or /* and */ to comment out a block of Javascript statments

Listing 1.3 A Basic Function <!--Hides scripts from really old browsers. function doSomething(){ var theVisitor = document.myform.visitor.value; window.alert("Is this OK, " + theVisitor + "?"); //document.write("Is this OK, " + theVisitor + "?"); } //ends script hiding --> Please type your name and click the button. COM311 H Zheng, School of C&M, UUJ 19 functions must be placed in head section!!

COM311H Zheng, School of C&M, UUJ20 Debugging JavaScript Basic Debugging Techniques

COM311 H Zheng, School of C&M, UUJ 21 Logic and Debugging Debugging Act of tracing and resolving errors in a program Three types of errors Syntax Invalid or incorrect statements Missing symbols (e.g., curly brace) Incorrect spelling or mistypes Using different case when referencing variables Run-time Call to function that hasn’t been defined Referencing a variable that hasn’t been declared Division by zero Logic Performing one operation when another is intended e.g., multiplying instead of dividing Infinite loops

COM311 H Zheng, School of C&M, UUJ 22 Debugging – show example in class In Firefox: Firefox produces the most sensible and helpful messages of current browsers Tools => Web Developer=> Error Console In IE versions higher than 4: Must turn on error notification Tools menu => Internet Options => Advanced In Browsing category: Select “Display a notification about every script error” In Safari, if you have web developer tool, you can use Error console too. In Opera: Tools => advanced => Error Console In Chrome, view => developer => Javascript console Messages don’t always identify the actual problem Don’t assume that the problem is only with the line number shown in the error message Tracing Errors with the alert() or write() Methods