Notes #.9: JavaScript Object For INFS 3510, Spring 2003.

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved JavaScript: Objects.
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript/Jscript 6 Objects. 2 Introduction Up till now –JavaScript used to illustrate basic programming concepts JavaScript can also –Manipulate every.
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
 2004 Prentice Hall, Inc. All rights reserved. Chapter 12 - JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object.
Introduction to scripting
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
1 JavaScript: Objects and Object Models October 25, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel,
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object 12.4 String Object Fundamentals of Characters and Strings.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
INE1020: Introduction to Internet Engineering 3: Web Page Authoring1 Lecture 6: Introduction to Javascript II r Javascript Objects Array String Date Math.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
 2008 Pearson Education, Inc. All rights reserved JavaScript: Objects.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 – JavaScript/Jscript: Objects Outline 13.1Introduction 13.2Thinking About Objects 13.3.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Advanced JavaScript Topics – Page 1 of 31CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Advanced JavaScript.
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. 2.
 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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 18 – JavaScript/Jscript: Objects Outline 18.1Introduction 18.2Thinking About Objects 18.3.
Session on Java script. Introduction JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet.
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 12: Objects CIS 275—Web Application Development for Business I.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 9. An event-driven, object-based programming language that provides various types of functionality to pages. Object based: it is a language that.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Java Script: Objects (Chapter 12 in [2]). 2 Outline Introduction Introduction Thinking About Objects Thinking About Objects Math Object Math Object String.
JavaScript Mimi Opkins CECS 470. What We’ll Cover Today u What is JavaScript? u What can it do? u How to program your pages using JavaScript u What do.
11 JavaScript: Objects.
Chapter 18 – JavaScript/Jscript: Objects
Chapter 6 JavaScript: Introduction to Scripting
Strings, StringBuilder, and Character
JavaScript Objects.
Introduction to Scripting
JavaScript: Objects.
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 12 - JavaScript: Objects
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
WEB PROGRAMMING JavaScript.
Chapter 12 - JavaScript: Objects
JavaScript: Objects.
Chapter 12 - JavaScript: Objects
JavaScript: Introduction to Scripting
11 JavaScript: Objects.
Presentation transcript:

Notes #.9: JavaScript Object For INFS 3510, Spring 2003

Chapter 18 – JavaScript/Jscript: Objects Objective To understand object-based programming (concepts, terminology, style) and its advantages To understand commonly used JavaScript objects Math String Date Boolean Number

Outline 18.1Introduction 18.2Thinking About Objects 18.3 Math Object 18.4 String Object Fundamentals of Characters and Strings Methods of the String Object Character Processing Methods Searching Methods Splitting Strings and Obtaining Substrings HTML Markup Methods 18.5 Date Object 18.6 Boolean and Number Objects

18.1 Introduction zUp till now yJavaScript used to illustrate basic programming concepts zJavaScript can also yManipulate every element of an HTML document from a script zIn this chapter yProvide more formal treatment of objects yOverview and serve as reference for xSeveral of JavaScript’s built-in objects xDemonstrates their capabilities

18.2 Thinking About Objects zJavaScript - object-based programming language zObjects yTwo categories xAnimate xInanimate yHas Attributes yHas Methods yEncapsulate data and methods yProperty: Information hiding yCommunicate with programs through interfaces zMost future software will be built by combining objects e.g. ActiveX

18.2 Thinking About Objects (II) zJavaScript uses objects to yInteract with elements (or objects) of an HTML document  window object Enables script to manipulate browser window window.status window.alert  document object Provides access to every element of an HTML document yEncapsulate various capabilities in a script  array object xEnables script to manipulate a collection of data

18.3 Math Object zCommonly used Math object properties

18.3 Math Object (II) Math Object Methods: allow programmer to perform many common mathematical calculations

18.4 String Object zString Object yJavaScript’s string and character processing capabilities zAppropriate for developing yText editors yWord processors yPage layout software yComputerized typesetting systems yOther kinds of text-processing software

Fundamentals of Characters and Strings zCharacters yFundamental building blocks of JavaScript programs zString ySeries of Characters treated as a single unit yMay include xLetters xDigits xSpecial Characters +, _, /, $, etc.

Fundamentals of Characters and Strings zString literals / string constant yWritten as sequence of characters in single or double quotation marks zStrings may be assigned to variables in declarations var color = “blue”; zStrings may be compared with yRelational operators yEquality operators

String Object Methods zProvides methods for ySelecting characters from a string yCombining strings (concatenation) yObtaining substrings of a string ySearching for substrings within a string yTokenizing a string yConverting strings to all uppercase or lowercase yGenerate HTML tags

String Object Methods (II)

Character Processing Methods

Character Processing Methods var s = "ZEBRA"; 10 var s2 = "AbCdEfG"; document.writeln( " Character at index 0 in '" + 18 s + "' is " + s.charAt( 0 ) ); 14 document.writeln( " Character code at index 0 in '" + 15 s + "' is " + s.charCodeAt( 0 ) + " " ); document.writeln( " '" + 18 String.fromCharCode( 87, 79, 82, 68 ) + 19 "' contains character codes 87, 79, 82 and 68 " ) document.writeln( " '" + s2 + "' in lowercase is '" + 22 s2.toLowerCase() + "'" ); 23 document.writeln( " '" + s2 + "' in uppercase is '" + 24 s2.toUpperCase() + "' " );

Script Output

Searching Methods zOften useful to search for character or sequence of characters in a string

Searching Strings with indexOf and lastIndexOf var letters = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; function buttonPressed() 12 { 18 searchForm.first.value = 14 letters.indexOf( searchForm.inputVal.value ); 15 searchForm.last.value = 16 letters.lastIndexOf( searchForm.inputVal.value ); 17 searchForm.first12.value = 18 letters.indexOf( searchForm.inputVal.value, 12 ); 19 searchForm.last12.value = 20 letters.lastIndexOf( searchForm.inputVal.value, 12 ); 21 }

34 First occurrence located at index Last occurrence located at index First occurrence from index 12 located at index Last occurrence from index 12 located at index The string to search is: 28 abcdefghijklmnopqrstuvwxyzabcdefghijklm 29 Enter substring to search for <INPUT NAME = "search" TYPE = "button" VALUE = "Search" 32 ONCLICK = "buttonPressed()"> 33

Script Output 1

Script Output 2

Splitting Strings and Obtaining Substrings zWhen you read a sentence yBreak it into individual words or tokens zProcess of breaking string into tokens is tokenization yAlso done by interpreters zTokens separated by delimiters yTypically white-space characters yOther characters can be used  Results of tokenization are displayed in HTML TEXTAREA GUI component

Splitting Strings and Obtaining Substrings (II)

String Method split and substring function splitButtonPressed() 10 { 11 var strings = myForm.inputVal.value.split( " " ); 12 myForm.output.value = strings.join( "\n" ); myForm.outputSubstring.value = 15 myForm.inputVal.value.substring( 0, 10 ); 16 }

Enter a sentence to split into words <INPUT NAME = "splitButton" TYPE = "button" VALUE = "Split" 25 ONCLICK = "splitButtonPressed()"> The sentence split into words is The first 10 characters of the input string are 32

Script Output

HTML Markup Methods

HTML Markup Methods of the String Object var anchorText = "This is an anchor", 10 bigText = "This is big text", 11 blinkText = "This is blinking text", 12 boldText = "This is bold text", 18 fixedText = "This is monospaced text", 14 fontColorText = "This is red text", 15 fontSizeText = "This is size 7 text", 16 italicText = "This is italic text", 17 linkText = "Click here to go to anchorText", 18 smallText = "This is small text", 19 strikeText = "This is strike out text", 20 subText = "subscript", 21 supText = "superscript"; 22

34 document.writeln( 35 " This is text with a " + subText.sub() ); 36 document.writeln( 37 " This is text with a " + supText.sup() ); 38 document.writeln( " " + linkText.link( "#top" ) ); document.writeln( anchorText.anchor( "top" ) ); 24 document.writeln( " " + bigText.big() ); 25 document.writeln( " " + blinkText.blink() ); 26 document.writeln( " " + boldText.bold() ); 27 document.writeln( " " + fixedText.fixed() ); 28 document.writeln( 29 " " + fontColorText.fontcolor( "red" ) ); 30 document.writeln( " " + fontSizeText.fontsize( 7 ) ); 31 document.writeln( " " + italicText.italics() ); 32 document.writeln( " " + smallText.small() ); 33 document.writeln( " " + strikeText.strike() );

Script Output

18.5 Date Object  JavaScript’s Date object yProvides methods for date and time manipulation zDate and time processing can be performed based on yLocal time zone yUniversal Coordinated Time (UTC) / Greenwich Mean Time (GMT)  Most methods in Date object have local time zone and UTC versions  When using Date object  Initialize Date object with current date and time var current = new Date();  Allocates memory for object, calls Date object constructor xConstructor – initializer method for an object

18.5 Date Object (II)  New Date object creation new Date( year, month, date, hours, minutes, seconds, milliseconds ); yHours, minutes, seconds and milliseconds are optional yIf argument to the right is specified, all arguments to the left must also be specified yMonth represented internally as integers from 0-11 xTherefore, March is indicated by 2, November by 10, etc. zWrite out years in 4-digit form (i.e. ‘2000’, not ’00’) yAvoid potential Y2K problems

18.5 Date Object (III)  Two other methods can be called without creating new Date object yBoth methods return number of milliseconds between midnight, January 1, 1970 and date specified by argument 1.Date.parse( argument ); yArgument xShort dates MM-DD-YY, MM-DD-YYYY, MM/DD/YY, MM/DD/YYYY xLong dates Month (at least first two letters), date and year Time in either 12 or 24 hour clocks Text and days of the week are ignored

18.5 Date Object (IV) 2. Date.UTC( argument ); yArgument - same for as date construct ( Y, M, D, H, M, S, M )  Either method can be converted to a Date object var theDate = new Date( numberOfMilliseconds );  numberOfMilliseconds equals the result of Date.UTC or Date.Parse  For listing of Date object methods, see Figure 18.8

Date and Time Methods var current = new Date(); document.writeln( 12 " String representations and valueOf " ); 18 document.writeln( "toString: " + current.toString() + 14 " toLocaleString: " + current.toLocaleString() + 15 " toUTCString: " + current.toUTCString() + 16 " valueOf: " + current.valueOf() ); document.writeln( 19 " Get methods for local time zone " ); 20 document.writeln( "getDate: " + current.getDate() + 21 " getDay: " + current.getDay() + 22 " getMonth: " + current.getMonth() + 23 " getFullYear: " + current.getFullYear() + 24 " getTime: " + current.getTime() + 25 " getHours: " + current.getHours() + 26 " getMinutes: " + current.getMinutes() + 27 " getSeconds: " + current.getSeconds() + 28 " getMilliseconds: " + current.getMilliseconds() + 29 " getTimezoneOffset: " + 30 current.getTimezoneOffset() ); 31

34 var anotherDate = new Date( 1999, 2, 18, 1, 5, 0, 0 ); 35 document.writeln( "Date: " + anotherDate ); document.writeln( 38 " Set methods for local time zone " ); 39 anotherDate.setDate( 31 ); 40 anotherDate.setMonth( 11 ); 41 anotherDate.setFullYear( 1999 ); 42 anotherDate.setHours( 23 ); 43 anotherDate.setMinutes( 59 ); 44 anotherDate.setSeconds( 59 ); 45 document.writeln( "Modified date: " + anotherDate ); document.writeln( 33 " Specifying arguments for a new Date " );

Script Output

18.6 Boolean and Number Objects  Boolean and Number objects yProvided as object wrappers for xBoolean true/false values xNumbers yWrappers define methods and properties useful in manipulating boolean values and numbers zNumber object  JavaScript automatically creates Number objects to store numeric values  Programmers can create a Number object with var n = new Number( numericValue );  For other Number object methods, see figure 18.11

18.6 Boolean and Number Objects (II)  Boolean object  When boolean value required in a program, automatically created by JavaScript to store the value using Boolean object  Programmers can create Boolean objects explicitly var b = new Boolean( booleanValue );  If booleanvalue equals false, 0, null, Number.NaN or empty string ( “ ” )  Boolean object contains false yOtherwise  Boolean Object contains true

18.6 Boolean and Number Objects (III) Methods of the Boolean Object