Arrays Create an array of strings var table = ["first", "second", "third"]; Print the third item alert(table[2]); Example IndexContents 0"first" 1"second"

Slides:



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

1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
The Web Warrior Guide to Web Design Technologies
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Functions and Methods Function : Block of instructions called (executed) by name Method: A function operating within an object –World Example: start car.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Introduction to JavaScript for Python Programmers
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
Javascript and the Web Whys and Hows of Javascript.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
by Chris Brown under Prof. Susan Rodger Duke University June 2012
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
JavaScript Lecture 6 Rachel A Ober
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Week 9 PHP Cookies and Session Introduction to JavaScript.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Objective Static vs. Dynamic Web pages. Variables. Assignments. JavaScript Hierarchy of Objects JavaScript Functions (prompt(“”,””) Document.write(“”)
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
HTML Concepts and Techniques Fifth Edition Chapter 6 Using Frames in a Web Site.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
How to make tables in HTML By Daniel Arze. How do they do this?
Logic and Control. 4-2 Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) { document.write(“ you are a fifteen”);
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Intro to JavaScript. Some simple examples Examples from our webpage Examples from Andrews webpage Today’s Example.
1 JavaScript
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
Making Websites with Dreamweaver BTT. What is Dreamweaver?  “What You See Is What You Get” (WYSIWYG) web design software  Rather than writing code and.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
Photoshop Image Slicing. Reasons to Image Slide To create links out of sliced images To optimise different areas. (flat areas of colour, such as logos,
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Tutorial 11 1 JavaScript Operators and Expressions.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
JavaScript, Third Edition 1 SELECTION LIST Demo. JavaScript, Third Edition 2 Description This web page will edit the slection to ensure an option was.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Cookies BIS1523 – Lecture 23.
CT Web Development, Colorado State University
WEB PROGRAMMING JavaScript.
Introduction to JavaScript for Python Programmers
JavaScript.
Web Programming and Design
Web Programming and Design
Presentation transcript:

Arrays Create an array of strings var table = ["first", "second", "third"]; Print the third item alert(table[2]); Example IndexContents 0"first" 1"second" 2"third" Definition: A table of variables accessed by its index Note: The initial index is zero; not one

Slogan of the Day var sloganList = ["Four score and seven years ago", "Don't worry, be happy", "At the end of the day", "Sooner rather than later", "He gives him some gravitas", "He needs the tools to combat terror"]; var num = Math.floor(Math.random()*sloganList.length); document.write(sloganList[num]); How would you add some more? Array: A table of variables that we access by its index

JavaScript Decision Statements if (** condition **) { **** block of instructions if condition is true**** } else { **** block of instructions if condition is false **** } Notes: 1.The braces are not needed if a block has only one instruction 2.The else part is optional, and can be skipped 3.Caution: syntax MUST be exact for it to work 4.The next slide describes what conditions are More commonly called condition statements

Conditions Examples of conditions Assume x=3, y=5, z=6, s1="abc", s2 = "def", s3="abcd" Possible Conditions A group of conditions x>y, x =x, x z, !(x>y), s1<s2, s1<s3 Are they true or false? false, true, true, false, true, true, false, false, true, true, true

Lets try a Rollover var firstImage = true; function rollOver(idName) { var tag = document.getElementById(idName); if (firstImage==true) { tag.src = "rabbit2.gif"; firstImage = false; } else { tag.src = "rabbit.gif"; firstImage = true; } } When the mouse goes over the image it switches The else part is optional and many if statements don't need it Note: == means equal

Lots of Rollovers var imageNo = 0; function rollOver(idName) { var tag = document.getElementById(idName); if (imageNo==0) tag.src = "rabbit1.gif"; else if (imageNo==1) tag.src = "rabbit2.gif"; else if (imageNo==2) tag.src = "rabbit3.gif"; else if (imageNo==3) tag.src = "rabbit4.gif"; else if (imageNo==4) tag.src = "rabbit5.gif"; else tag.src = "rabbit.gif"; imageNo = (imageNo + 1) % 6; } It switches between six rabbit pictures on clicks Substitute Math.floor((Math.random()) * 6) Instead of: (imageNo+1)%6 to make the pictures switch randomly The % means remainder 10 % 3 = 1

Rollovers Using an Array var num = 0; var gifNames = ["rabbit.gif", "rabbit1.gif", "rabbit2.gif", "rabbit3.gif ", "rabbit4.gif", "rabbit5.gif"]; function rollOver(idName) { var tag = document.getElementById(idName); tag.src = imageList[num].src; num = (num+1)%imageList.length; } JavaScript calls these tables arrays Definition: An array is an indexed table of variables

Guess the Number Game var theNumber = Math.floor(Math.random()*1000); function guessIt() { var textBox = document.getElementById("guess"); var answer = parseInt(textBox.value); if (isNaN(answer)) alert("Illegal Input"); else if (answer < theNumber) alert("Too Low"); else if (answer > theNumber) alert("Too High"); else if (answer == theNumber) { alert("You got it"); theNumber = Math.floor(Math.random()*1000); } return; }

An On-line Quiz

JavaScript Slide Show Create your slides. You can do this with PowerPoint and use the "save as jpg" or "save as png" options. Create a web-page to roll over on a mouse click. –Simply create a form with a submit button –When the button clicks, then switch to the next picture Additional Comments –When you do this, you are using the techniques we described on the previous slides. –An entire slide show is possible with a single html page –Once done once, you can create other slide shows by just linking to other images –Visitors to your pages don't need to have PowerPoint installed.

Detect Browser type and version document.write('Enjoy your web experience with '); document.write(navigator.appName + ' ' +navigator.appVersion); // A warning message for viewers with old web browsers if ( parseInt( navigator.appVersion) ' + ' Your browser is out of date '); }

Create a Pop Up Window function showPopup() { // Create a new popup window var popup = window.open( "", "popup", "width=600,height=450," + "status,scrollbars,resizable, screenX=20,screenY=40, left=20, top=40"); // Write HTML into popup window page using document.writeln statements popup.document.writeln(' Nice Page '); popup.document.writeln(' '); popup.document.writeln(' Big Header '); popup.document.writeln(' '); // Tell browser that we are done and make the popup visible popup.document.close(); popup.focus() } Note: Use the first argument of window.open for displaying web pages that already exist; in this case, popup.doument.writeln statements aren’t needed. Note: The third argument configures how the popup will look; some of its values are duplicates so the popup will work with all browsers

Display the Time of the Day var today = new Date(); var hours = today.getHours(); var pm = "p.m.“; if (hours < 12) pm = "a.m.“; hours = hours % 12; if (hours==0) hours=12; document.write (hours + ":" + today.getMinutes() + ":" + today.getSeconds() + " " + pm + " "); We are creating a new date object

Review Questions When is a '{' needed in an IF statement? What is an array? What is a condition? How do you represent 'not equal' in JavaScript? What is the difference between '==' and not '=‘? How can JavaScript create a popup window? What does NaN mean? What statement randomly picks an integer 5 and 10? How do we write tags into a browser window? What does the Math.floor() function do?