Docsity

Introduction to Scripting - Introduction to Java Script - Lecture Slides, Slides of Javascript programming

Partial preview of the text

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

JavaScript: Introduction to Scripting

Topics Covered

  • Writing simple JavaScript programs.
  • Using input and output statements
  • Basic memory concepts.
  • Arithmetic operators.
  • Decision-making statements.
  • Relational and equality operators.

JavaScript: Object-Based Language

  • There are three object categories in JavaScript: Native Objects, Host Objects, and User-Defined Objects. - Native objects: defined by JavaScript. - String, Number, Array, Image, Date, Math, etc. - Host objects : supplied and always available to JavaScript by the browser environment. - window, document, forms, etc. - User-defined objects : defined by the author/programmer
  • Initially, we will use host objects created by the browser and their methods and properties

Scripting

  • Two approaches to client side scripting:
    • Inline scripting
      • Written in the section of a document
    • JavaScript code embedded in the section

welcome.html

(1 of 1)

1 2 4 5 6 7 8 9 10 A First Program in JavaScript 11 12 18 19 20

HTML comment tags will

result in skipping of the script

by those browsers that do not

support scripting

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

Escape character in combination with quotation mark: \” will result in insertion of a quotation mark in the string that is actually written by JavaScript

welcome4.html

1 of 1

1 2 4 5 6 7 8 9 Printing Multiple Lines in a Dialog Box 10 11 16 17 18 19 20

Click Refresh (or Reload) to run this script again.

21 22

alert method of the object displays a Dialog

Dynamic Pages

  • A script can adapt the content based on explicit input from

the user or other information

  • System clock: Time of day
  • Hidden input
  • Cookies
  • User input can be collected by invoking the prompt method

of a window object

  • This will display a dialog box that prompts user for input

welcome5.html

(1 of 2)

1 2 4 5 6 7 8 9 10 Using Prompt and Alert Boxes 11 12

JavaScript is a loosely typed language. Variables take

 on any data type depending on the value assigned.

 Value returned by the prompt

method of the window object is

assigned to the variable name

“+” symbol can be used for text concatenation as well as arithmetic operator

Fig. 7.7 Prompt dialog displayed by the window object’s prompt method.

This is the prompt to the user.

This is the default value that appears when the dialog opens.

This is the text field in which the user types the value.

When the user clicks OK , the value typed by the user is returned to the program as a string.

If the user clicks Cancel , the null value will be returned to the program and no value will be assigned to the variable.

Simple Script Example: Adding Integers

  • The values of numbers to be added are obtained as user inputs colleted through the window.prompt method
  • parseInt
    • Converts its string argument to an integer
    • What happens if the conversion is not done?
      • See example on our web site
  • NaN (not a number): value returned if non-numerical values are passed to the paresInt method

Addition.html

(2 of 2)

24 // read in second number from user as a string 25 secondNumber = 26 window.prompt( "Enter second integer", "0" ); 27 28 // convert numbers from strings to integers 29 number1 = parseInt( firstNumber ); 30 number2 = parseInt( secondNumber ); 31 32 // add the numbers 33 sum = number1 + number2; 34 35 // display the results 36 document.writeln( "The sum is " + sum + "" ); 37 // --> 38 39 40 41 42

Click Refresh (or Reload) to run the script again

43 44