JavaScript: Control Structures - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

JavaScript: Control Structures

Description:

ISBN 0-13-145091-3 Chapter 7 - JavaScript: Introduction to Scripting Objectives In this lesson, you will learn: To be able to write simple JavaScript programs. – PowerPoint PPT presentation

Number of Views:285
Avg rating:3.0/5.0
Slides: 34
Provided by: Bei71
Category:

less

Transcript and Presenter's Notes

Title: JavaScript: Control Structures


1
JavaScript Control Structures
  • September 27, 2005
  • Slides modified fromInternet World Wide Web
    How to Program. 2004 (3rd) edition. By Deitel,
    Deitel, and Goldberg. Published by Prentice Hall.
    ISBN 0-13-145091-3

2
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
Obtaining User Input with prompt
Dialogs 7.3.1 Dynamic Welcome Page 7.3.2 Adding
Integers 7.4 Memory Concepts 7.5 Arithmetic 7.6
Decision Making Equality and Relational
Operators 7.7 Web Resources
3
Objectives
  • In this lesson, you will learn
  • To be able to write simple JavaScript programs.
  • To be able to use input and output statements.
  • To understand basic memory concepts.
  • To be able to use arithmetic operators.
  • To understand the precedence of arithmetic
    operators.
  • To be able to write decision-making statements.
  • To be able to use relational and equality
    operators.

4
Introduction to JavaScripting
5
7.1 Introduction
  • JavaScript scripting language
  • Enhances functionality and appearance
  • Client-side scripting
  • Makes pages more dynamic and interactive
  • Foundation for complex server-side scripting
  • Program development
  • Program control

6
What can JavaScript programs do?From
http//www.javascripter.net/faq/javascri.htm
  • Giving the user more control over the browser
  • Detecting the user's browser, OS, screen size,
    etc.
  • Performing simple computations on the client side
  • Validating the user's input
  • Handling dates and time
  • Generating HTML pages on-the-fly without
    accessing the Web server.

7
What cant JavaScript programs do?From
http//www.javascripter.net/faq/javascr2.htm
  • Use printers or other devices on the user's
    system or the client-side LAN
  • Directly access files on the user's system or the
    client-side LAN the only exception is the
    access to the browser's cookie files.
  • Directly access files on the Web server.
  • Implement multiprocessing or multithreading.

8
Programming context
  • Interpreted language
  • Web browser as the interpreter
  • Client-side only
  • Compatibility depends on browser version
  • But virtually every browser supports JavaScript
  • Follow ECMAScript standard!
  • Security issues
  • You cannot access the clients system beyond
    their web browser
  • Thus, no file access or direct printing

9
Notes on compatibility
  • Browser versions
  • 80-90 of people user Internet Explorer
  • Of which 99 use IE 6 or IE 5
  • 10-15 use Gecko-based browsers
  • Firefox, Netscape, Mozilla
  • JavaScript and cookies
  • Only 85-90 have JavaScript enabled
  • Cookies usually enabled by defaultI dont have
    statistics for how many leave this on
  • Screen resolutions
  • 25 use 800x600
  • 70 use 1024x768 or higher
  • Recommendations
  • Test all pages with IE 5/6 and Firefox
  • Dont assume that users have JavaScript or
    cookies enabled
  • Your site should still be functional without
    client-side scripting or cookies
  • Design to be comfortably viewable with 800x600
    resolution
  • Sources
  • http//www.upsdell.com/BrowserNews/stat_trends.htm
  • http//www.w3schools.com/browsers/browsers_stats.a
    sp

10
Object orientation in JavaScript
  • JavaScript is an object-oriented programming
    language
  • Based on objects with properties and methods
  • JavaScript uses prototyping to replicate
    behaviour, rather than classification (classes)
  • There are two types of variables
  • Simple variables (also called primitives)
  • Objects

11
Objects in JavaScript
  • An object is a complex type of variable that
    contains its own variables (properties), and has
    its own functions (methods)
  • Properties
  • The variables of an object
  • Can also be other objects
  • Methods
  • The functions that belong to an object
  • e.g. document.bgColor, window.location
  • JavaScript has a rich set of built-in objects
  • http//www.devguru.com/Technologies/ecmascript/qui
    ckref/js_objects.html
  • You can also define your own objects

12
How to specify JavaScript code
  • Inline Code
  • Add dynamic functionality to an individual
    element
  • ltimg onMouseOverdoThisFunction(this)gt
  • Write JavaScript code directly in appropriate
    event attributes
  • Embedded scripts
  • Embed JavaScript code in an XHTML documenteither
    in the head or in the body
  • ltscript typetext/javascriptgt x3 y9
    document.writeln( x y ) lt/scriptgt
  • Inserting external scripts
  • Insert a file with .js extension that contains
    JavaScript code
  • ltscript typetext/javascript srcexternal.js
    /gt
  • JavaScript libraries can be shared by multiple
    XHTML documents
  • A note on CDATA
  • lt!CDATA gt
  • Necessary for legal XML to ignore and lt symbols
    (which are common in scripts)
  • However, most browsers work fine without them
  • http//www.w3schools.com/xml/xml_cdata.asp
  • The textbook says to use lt!-- comments to hide
    your code --gt
  • But CDATA is proper XML syntax
  • Comments should validate, but not guaranteed

13
First scripts
14
7.2 Simple Program Printing a Line of Text in a
Web Page
  • Inline scripting
  • Written in the ltbodygt of a document
  • ltscriptgt tag
  • Indicate that the text is part of a script
  • type attribute
  • Specifies the type of file and the scripting
    language use
  • writeln method
  • Write a line in the document
  • Escape character ( \ )
  • Indicates special character is used in the
    string
  • alert method
  • Dialog box

15
welcome.html(1 of 1)
16
welcome2.html(1 of 1)
17
welcome3.html1 of 1
18
welcome4.html1 of 1
19
Illegal characters inXHTML vs. JavaScript
  • XHTML
  • HTML entities
  • amp lt gt
  • Others http//www.w3schools.com/tags/ref_entities
    .asp
  • JavaScript
  • Escape sequences
  • \ \ \n \t \\
  • Others http//www.devguru.com/Technologies/ecmasc
    ript/quickref/escaped_characters.html
  • To convert between the two,use an XHTML ?
    JavaScript converter
  • Google HTML JavaScript converter
  • Best that Ive found
  • http//javascript.about.com/library/blscr02.htm

20
7.2 Common escape sequences
For full list, see http//www.devguru.com/Technolo
gies/ecmascript/quickref/escaped_characters.html
21
7.3.1 Dynamic Welcome Page
  • A script can adapt the content based on input
    from the user or other variables

22
welcome5.html(1 of 2)
23
7.3.1 Dynamic Welcome Page
This is the text field in which the user types
the value.
Fig. 7.7 Prompt dialog displayed by the window
objects prompt method.
24
JavaScript data types
  • In JavaScript, you can use the var statement to
    create a variable, but it is not required
  • You do not declare any data type (loosely typed)
  • For scripting languages, data types are
    automatically determined
  • Automatically converts between values of
    different types
  • JavaScript data types
  • Numbers
  • Numbers are numbers, whether integer or floating
    point
  • Boolean values
  • true or false
  • Strings
  • Anything within quotes
  • Anything within quotes
  • Null
  • "special key word denoting a null value"
  • Means no value
  • From http//www.ryerson.ca/JavaScript/lectures/bas
    icDataTypes/basicTypes.html

25
7.3.2 Adding Integers
  • Prompt user for two integers and calculate the
    sum (Fig. 7.8)
  • NaN (not a number)
  • parseInt
  • Converts its string argument to an integer

26
Addition.html(1 of 2)
27
JavaScript native functions
  • See http//www.devguru.com/Technologies/ecmascript
    /quickref/js_functions.html
  • parseInt()
  • Converts a string into an integer, if possible
  • parseFloat ()
  • Converts a string into a floating-point number,
    if possible
  • string ()
  • Converts any object into a string
  • number ()
  • Converts any object into a number, if possible
  • isNAN ()
  • true if the object is Not A Number
  • escape() and unescape()
  • Converts and deconverts strings to safe
    characters

28
Operators in JavaScript
  • http//www.devguru.com/Technologies/ecmascript/qui
    ckref/js_operators.html

29
7.5 Arithmetic operators
30
7.6 Decision Making Equality and Relational
Operators
?

31
welcome6.html(1 of 3)
32
welcome6.html(2 of 3)
33
7.6 Decision Making Equality and Relational
Operators
Write a Comment
User Comments (0)
About PowerShow.com