Docsity

Control Structures - Introduction to Java Script - Lecture Slides, Slides of Javascript programming

Partial preview of the text

Download Control Structures - Introduction to Java Script - Lecture Slides and more Slides Javascript programming in PDF only on Docsity!

JavaScript: Control Structures

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

Introduction to JavaScripting

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

What can’t 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.

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 client’s system beyond their web browser
        • Thus, no file access or direct printing

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

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/quickref/js_objects.html
  • You can also define your own objects

First scripts

7.2 Simple Program: Printing a Line of

Text in a Web Page

  • Inline scripting
    • Written in the of a document

welcome2.html

(1 of 1)

1 2 4 5 6 7 8 9 10 Printing a Line with Multiple Statements 11 12 19 20 21

welcome3.html

1 of 1

1 2 4 5 6 7 8 9 Printing Multiple Lines 10 11 17 18 19

Illegal characters in

XHTML vs. JavaScript

  • XHTML
    • HTML entities
      • & < >
      • Others: http://www.w3schools.com/tags/ref_entities.asp
  • JavaScript
    • Escape sequences
      • \’ \” \n \t \
      • Others: http://www.devguru.com/Technologies/ecmascript/quickref/escaped_characters.html
  • To convert between the two,

use an XHTML  JavaScript converter

  • Google “HTML JavaScript converter”
  • Best that I’ve found:
    • http://javascript.about.com/library/blscr02.htm

7.2 Common escape sequences

Escape sequence Description \n (^) Newline. Position the screen cursor at the beginning of the next line. \t (^) Horizontal tab. Move the screen cursor to the next tab stop. \r (^) Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. \ (^) Backslash. Used to represent a backslash character in a string. " (^) Double quote. Used to represent a double quote character in a string contained in double quotes. For example, window.alert( ""in quotes"" ); displays "in quotes" in an alert dialog.

' (^) Single quote. Used to represent a single quote character in a string. For example, window.alert( ''in quotes'' ); displays 'in quotes' in an alert dialog.

Fig. 7.5 Some common escape sequences.

For full list, see http://www.devguru.com/Technologies/ecmascript/quickref/escaped_characters.html