JavaScript – loops and arrays. Arrays as cubbyholes  Array is an ordered collection of data  The content in the array is sometimes known as the array.

Slides:



Advertisements
Similar presentations
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.
Advertisements

The Web Warrior Guide to Web Design Technologies
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
JavaScript- Processing HTML Forms. Event Handlers Begins with and ends with.
JavaScript- Introduction. What it is and what it does? What it is? It is NOT Java It is NOT Server-side programming Users can see code It is a client-side.
Text Box controls are used when users are required to type some input (during program execution), or output is displayed on the form (known as the user-
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Web-based Application Development Lecture 13 February 21, 2006 Anita Raja.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Web-based Application Development Lecture 18 March 21, 2006 Anita Raja.
JavaScript, Third Edition
Introduction to Visual Basic Chulantha Kulasekere.
Introduction to JavaScript for Python Programmers
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
CST JavaScript Validating Form Data with JavaScript.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
INTERNET APPLICATION DEVELOPMENT For More visit:
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
The Bean Counter: A JavaScript Program
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
GEMVC. The Setup Folders Views Value Objects (VOs) Custom Events Service CFCs Controller Model Application Main MXML.
CSCE 102 – Chapter 9 (Functions and Parameters) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
Document Objects Forms, Images and Links. The document object model Each element of an HTML document, such as an image, form, link, or button, can be.
JavaScript – more arrays, images. Image Object and images[] array  Using allows us to display images on webpages. What if we want more control of what.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
JavaScript, Fourth Edition
JavaScript – return function, time object and image rollover.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Understanding JavaScript and Coding Essentials Lesson 8.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript 101 Lesson 6: Introduction to Functions.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Programming the Web using XHTML and JavaScript
JavaScript Arrays Date
A (gentle???) Introduction to JavaScript
MON TUE WED THU
JavaScript onLoad arrays
2008 Calendar.
I dragged over the label tool (A icon) and put it on the form.
Sun Mon Tue Wed Thu Fri Sat
Introduction to Programming and JavaScript
Sun Mon Tue Wed Thu Fri Sat
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Web Programming and Design
2008 Calendar.
Retrieving information from forms
Presentation transcript:

JavaScript – loops and arrays

Arrays as cubbyholes  Array is an ordered collection of data  The content in the array is sometimes known as the array elements  You reference an array using its index. So if there are 10 cubbyholes in the array, it is index from 0 to 9.

Create an Array – declaration  In order for you to use an array, you need to declare an array. You need to know prior to declaration, what size you want ie. how many cubbie-holes you want in this array. var sampleArray = new Array(7) or var daysOfWeek = new Array(7)

Putting values in cubbie-holes. var daysOfWeek = new Array(7) /* declaration */ daysOfWeek[0] = "Mon"; daysOfWeek[1] = "Tue"; daysOfWeek[2] = "Wed"; daysOfWeek[3] = "Thu"; daysOfWeek[4] = "Fri"; daysOfWeek[5] = "Sat"; daysOfWeek[6] = "Sun"; /* declaration and initial assignment */ var daysOfWeek = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");

Loops (remember VB) – for, while The syntax for loops are: for (i=0; i<7; i+=) { // instructions here } For example to flash message box of the days: for (i=0; i<7; i++) { alert("Today is "+ daysOfWeek[i]); } Or you could take one more step and do the following instead: for (i=0; i<7; i++) { showDay = daysofWeek[i]; alert("Today is "+ showDay); /* you are more comfortable this way */ } For example to initialize an array to all 0s: var sampleArray = new Array(7); for (i=0; i<7; i++) { sampleArray[i]=0; }

Loops – for, while The syntax for while loops is while (count<7) { // instructions here count++; // increment count } The syntax for do while loops is do { // instructions here count++; // increment count } while (count<7)

elements[n]  In the POP/TART exercise when we reset the “values” for the button, we physically go to each button ie. b1.value= "" and b2.value= "" and so on.  There is an easy way using arrays.  When a form is loaded on the browser, an element array was created (without telling you) in the background. So instead of referencing it by its name ie. b1.value, you can use the generic reference array: elements[n].value where n is a number from 0 onwards depending on the order of how the element is arranged. So the the first element would be document.formName.elements[0].value and the next one would be document.formName.elements[1].value and so on.

Example using element[ ] While you reset the buttons, you might want to use a “for” loop to reset the values; document.simpleForm.elements[i].value= ""; However, if you are not sure which element index goes with which GUI, you might want to just simply do this document.simpleForm.elements[i].value=i; (for i=0, i < n, i++) where n is the number of GUI objects (buttons, input text, message, labels etc) that you created. Once you know which index goes with which object, you can now perform a for loop to “reset” the values of the buttons and labels.

Illustration of element[]

Pull-down menu with button function change(){ var i=document.colorform.colormenu.selectedIndex; document.bgColor=document.colorform.colormenu.options[i].value; } // flint violet dusk clover carnation subtle pink

Pull-down menu without button flint violet dusk clover carnation subtle pink This is new

Exercise: jex9.html  Using the code from the previous two examples. Create the following page. Make sure you name the two menu with different names. (WARNING: DO NOT JUST COPY AND PASTE WITHOUT KNOWING THIS)

Image Object and images[] array  Using allows us to display images on webpages. What if we want more control of what to show?  When a webpage with images is loaded by a browser, an images[] array is created. The first image is loaded onto images[0] and so on.  Unlike, GUI such as buttons etc, where they are the properties of the FORM, images are properties of the DOCUMENT.  In order to display an image using javascript. The command is straight forward as such. document.images[0].src= "abc.gif"

Object Hierarchy for images

Simple Example function changeCreate() { document.images[0].src="create.gif" } function changeImpact() { document.images[0].src="impact.gif" } function changeDrive() { document.images[0].src="drive.gif" } function changeDiscover() { document.images[0].src="discover.gif" }

Pre-loading of images to arrays  You can pre-load your image into arrays and when you need a particular image, you can assign  First a new array has to be declared: myPic = new Array();  Then you need to write a loop to define or construct the image object for each array element for (i=0; i<n; i++) { myPic[i] = new Image(); // make sure Image is ‘I’ }  Note that n is the number of pictures you like to pre-load.  You can then define myPic[0].src = " pic1.gif " ; myPic[1].src = " pic2.gif " ;

Simple Example - revised:jex10.html // declare array var myPic=new Array(); //declare image objects for each array for (i=0; i<4;i++) { myPic[i]=new Image(); } // preload the pictures myPic[0].src="create.gif" myPic[1].src="impact.gif" myPic[2].src="drive.gif" myPic[3].src="discover.gif" function changeCreate() { document.images[0].src=myPic[0].src; } function changeImpact() { document.images[0].src=myPic[1].src; } function changeDrive() { document.images[0].src=myPic[2].src; } function changeDiscover() { document.images[0].src=myPic[3].src; } same as before ….

The onload event handler  The onload handler says that when the browser loads the page, you can execute the javaScript function immediately

Example of onload handler function disFirst() { // load the first image in my array document.images[0].src=myPic[0].src; } <INPUT type=button value="DISCOVER" onclick="changeDiscover()" Here is displaying an image using a command even though The image file name is “”

Small exercise: random picture: jex11.html  You will create a similar webpage, except that the first image that is pre-loaded will not be fixed. Previously it was set to myPic[0].src  Use the Math.random() function that returns So if you want a number between 0 to 3 (for 4 pictures). You will do the following k=Math.round(Math.random()*3);  Now that you have the index k, you can apply to myPic[k].src to get a random picture.

Assignment 2 due soon! Lesson Learnt: All of us will be spiderman or spiderwoman at the end of the semester.