Docsity

Creating a Programmable Web Page - Introduction to Java Script - Lecture Slides, Slides of Javascript programming

Partial preview of the text

Download Creating a Programmable Web Page - Introduction to Java Script - Lecture Slides and more Slides Javascript programming in PDF only on Docsity!

JavaScript

Creating a Programmable Web Page

Tutorial Objectives

  • Understand basic JavaScript syntax
  • Create an embedded and external script
  • Work with variables and data
  • Work with data objects and extract values from dates
  • Work with expressions and operators
  • Create and call a JavaScript function
  • Work with arrays and conditional statements
  • Learn about program loops

Client-Side Programs

  • solve many of the problems associated with server-side

scripts

  • can be tested locally without first uploading it to a Web

server

  • are likely to be more responsive to the user
  • can never completely replace server-side scripts

Introduction to JavaScript

  • JavaScript is an interpreted programming or script language from Netscape.
  • JavaScript is used in Web site development to such things as:

 automatically change a formatted date on a Web page

 cause a linked-to-page to appear in a popup window  cause text or a graphic image to change during a mouse rollover

ECMAScript

  • The responsibility for the development of a scripting
standard has been transferred to an international body
called the European Computer Manufacturers Association
(ECMA).
  • The standard developed by the ECMA is called
ECMAScript, though browsers still refer to it as JavaScript.
  • The latest version is ECMA-262, which is supported by the
major browsers.

Other Client-side Languages

  • Internet Explorer supports JScript.
  • JScript is identical to JavaScript, but there are some

JavaScript commands not supported in JScript, and vice

versa.

  • Other client-side programming languages are also

available to Web page designers, such as the Internet

Explorer scripting language, VBScript.

Writing a JavaScript Program

  • The Web browser runs a JavaScript program when the Web page is first loaded, or in response to an event.
  • JavaScript programs can either be placed directly into the HTML file or they can be saved in external files. - placing a program in an external file allows you to hide the program code from the user - source code placed directly in the HTML file can be viewed by anyone

Writing a JavaScript Program

  • A JavaScript program can be placed anywhere within the HTML file.
  • Many programmers favor placing their programs between **** tags in order to separate the programming code from the Web page content and layout.
  • Some programmers prefer placing programs within the body of the Web page at the location where the program output is generated and displayed.

Comments

  • The syntax for a single-line comment is:

// comment text

  • The syntax of a multi-line comment is:

comment text covering several lines

Hiding Script from Older Browsers

  • You can hide the script from these browsers using comment tags: ****
  • When a Web browser that doesn’t support scripts encounters this code, it ignores the **

JavaScript Syntax Issues

  • JavaScript commands and names are case-sensitive.
  • JavaScript command lines end with a semicolon to

separate it from the next command line in the program.

  • in some situations, the semicolon is optional
  • semicolons are useful to make your code easier to follow and interpret

Working with Variables & Data

  • A variable is a named element in a program that stores information. The following restrictions apply to variable names: - the first character must be either a letter or an underscore character ( _ ) - the remaining characters can be letters, numbers, or underscore characters - variable names cannot contain spaces
  • Variable names are case-sensitive.
  • document.write(Year);

Declaring a Variable

  • Before you can use a variable in your program, you need
to declare a variable using the var command or by
assigning the variable a value.
  • Any of the following commands is a legitimate way of
creating a variable named “Month”:
var Month;
var Month = “December”;
Month = “December”;

Working with Dates

  • There are two ways to create a date object: variable = new Date (“month day, year, hours:minutes: seconds”) variable = new Date (year, month, day, hours, minutes, seconds”) - variable is the name of the variable that contains the date information - month , day , year , hours , minutes , and seconds indicate the date and time var Today=new Date(“October 15, 2006”); var Today=new Date(2006, 9 , 15);