introducing javascript. server-side and client-side programming server-side programming –program...

45
Introducing JavaScript

Upload: cade-meachum

Post on 01-Apr-2015

239 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Introducing JavaScript

Page 2: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Server-Side and Client-Side Programming

• Server-side programming– Program placed on server that hosts Web site– Program then used to modify contents and

structure of Web pages

Page 3: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Server-Side and Client-Side Programming

• Client-side programming– Runs programs on user’s computer – Programs likely to be more responsive to users– complement server-side programming

Page 4: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Server-Side Programming

Page 5: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Client-Side Programming

Page 6: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Combining Client-Side and Server-Side Programming

Page 7: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Introducing JavaScript

• Server-side Programs pose problems

• Client-side Programs were developed to run programs and scripts on the client side of a Web browser

Page 8: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

The Development of JavaScript

• Java – Developed by Sun Microsystems

– Programs designed to be run within Java interpreters

– An example of a compiled language

• JavaScript– Developed by NETSCAPE,

– An interpreted language

– Internet Explorer supports a version called JScript

Page 9: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Comparing Java and JavaScript

Page 10: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Versions of JavaScript

Page 11: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

The Development of JavaScript

• Jscript is a version of JavaScript supported by Internet Explorer

• The European Computer Manufacturers Association (ECMA) develops scripting standards– The standard is called ECMAScript but

browsers still generally call is JavaScript

Page 12: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Inserting JavaScript into a Web Page File

• Outline the main tasks you want the program to perform first

• A JavaScript program can either be placed directly in a Web page file or saved in an external text file

Page 13: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Inserting JavaScript into a Web Page File

• Insert a client-side script in a Web page when using the script element

• Comments are useful for hiding scripts from older browsers

• Specify alternative content using the nonscript element for browsers that don’t support scripts (or have their script support disabled)

Page 14: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Writing Output to the Web Page

• An object-based programming language writes the output by manipulating tasks

• An action you perform on an object is called a method

Page 15: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Writing Output to the Web Page• To write text to a Web page, use the following

JavaScript commands:document.write(“text”);

or

document.writeln(“text”)’Where text is the content to be written to the page. The doucment.write() and document.writeln() methods are

identical, except that the document.writeln() method preserves any line

breaks in the text string.

Page 16: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with the Script Element

• Script element– Used to enter scripts into an HTML or XHTML

file– Syntax

<script type="mime-type">

script commands

</script>

Page 17: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Writing Output to a Web Document

• Inserting [email protected] in a Web document

<script type="text/javascript">

document.write(“[email protected]");

</script>

Page 18: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

The document.write() Method

• One way to send output to the Web document

• Object– Can be any item, including mouse pointer or

window scrollbars

• Method– Process by which JavaScript manipulates the

features of an object

Page 19: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Understanding JavaScript Rules and the Use of White Space

• JavaScript – Is case sensitive– Ignores most occurrences of extra white space– Line breaks occurring within a statement can

cause error– Good practice to not break a statement into

several lines

Page 20: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Supporting Non-JavaScript Browsers

• noscript element– Used by browsers that do not support scripts– Syntax

<noscript>

alternative content

</noscript>

Page 21: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Variables

• Variable – A named item in a program that stores

information– Used to represent values and text strings– Values can change as the program runs

Page 22: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Variables and Data

• JavaScript variable types:– Numeric variables– String variables– Boolean variables– Null variables

• You declare a variable before using it

Page 23: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Variables and Data

• Numeric variable- any number, such as 13, 22.5, etc– Can also be expressed in scientific notation

• String variable- any group of text characters, such as “Hello” or “Happy Holidays!”– Must be enclosed within either double or single quotations

(but not both)

• Boolean variable- accepts only true and false values• Null variable- has no value at all

Page 24: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Declaring a Variable

• Tells JavaScript interpreter to reserve memory space for the variable

• Statement to declare a variablevar variable;

• Declaring three variablesvar emLink, userName, emServer;

Page 25: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Declaring a JavaScript Variable

• You can declare variables with any of the following JavaScript commands:var variable;

var variable = value;

variable = value;

Where variable is the name of the variable and value is the initial value of the variable. The first command creates the variable without assigning it a value; the second and third commands both create the variable and assign it a value.

Page 26: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Declaring a Variable

• Limits on variable names– First character must be either a letter or an

underscore character ( _ )– Remaining characters can be letters, numbers,

or underscore characters– Variable names cannot contain spaces– Reserved words cannot be used

Page 27: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Writing a Variable Value to a Web Document

• Variable– Can be used in place of value it contains

• Writing a text string to a Web pagevar libName = ”Duston Public Library";document.write(libName);

• Plus symbol ( + ) – Can be used to combine variable with text

string

Page 28: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Dates• Create a date object to store date information

Date Methods

Page 29: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Expressions and Operators

• Expressions are JavaScript commands that assign values and variables

• Operators are elements that perform actions within expressions

– Arithmetic operators: perform simple mathematical calculations

– Binary operators: work on two elements in an expression

– Unary operators: work on only one variable

– Increment operators: can be used to increase the value of a variable by 1

– Assignment operators: used to assign values in expressions

Page 30: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Working with Expressions and Operators

• The Math object is a JavaScript object used for calculations other than simple math

Page 31: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Creating a Function to Perform an Action

• Functions– Collection of commands that perform an action

or return a value– Include a function name– Include a set of commands that run when

function is called– Some require parameters

Page 32: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Creating a Function to Perform an Action

• Syntax of a JavaScript functionfunction function_name(parameters){

JavaScript commands

}

• Calling a functionfunction_name(parameter values)

Page 33: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Functions and Variable Scope

• Variable scope– Indicates where and how the variable can be

used in your application– Can be local or global

Page 34: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Functions and Variable Scope

• Local scope– Variable created within a JavaScript function

• Global scope– Variables not declared within functions

Page 35: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Creating a Function to Return a Value

• For a function to return a value– It must include a return statement

• Syntax of a function that returns a valuefunction function_name(parameters) {

JavaScript commands

return value;

}

Page 36: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Commenting JavaScript Code• Comments

– Explain what your programs are designed to do and how they work

• Multiline comment/*

The showEM() function displays a link to the user’s e-mail address.

The username and e-mail server name are entered in reverse order

*/

Page 37: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Using Comments to Hide JavaScript Code

• Syntax for hiding script<script type="text/javascript">

<!-- Hide from non-JavaScript browsers

JavaScript commands

// Stop hiding from older browsers -->

</script>

Page 38: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Accessing an External JavaScript File

• Common practice to – Create libraries of functions located in external files

• Script elements that point to external files are– Placed in a document’s head section

• Extension “.js”– Used by external files containing JavaScript commands

and functions

Page 39: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Using an External Script

Page 40: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Debugging Your JavaScript Programs

• Three types of errors:– Load-time errors (occurs when the script is

loading)– Run-time errors (occurs when the being

executed)– Logical errors (free from syntax and structural

mistakes, but result in incorrect results)

Page 41: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Common Mistakes

• You need to debug your program to fix the mistakes

• Common mistakes include:– Misspelling a variable name

– Mismatched parentheses or braces

– Mismatched quotes

– Missing quotes

– Using ( instead of [

– Using = in place of ==

Page 42: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Debugging Tools and Techniques

• To avoid making mistakes and quickly located those you do make:– Write modular code– Use the Microsoft Script Debugger to debug

(for use with Internet Explorer)– Use the JavaScript Console to debug (for use

with Netscape and Mozilla family of browsers)

Page 43: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Microsoft Debugger Window

Page 44: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Tips for Writing Good JavaScript Code

• Use good layout to make your code more readable. Indent command blocks to make them easier to read and to set them off from other code

• Use descriptive variable names to indicate the purpose of your variables

• Be careful how you use uppercase and lowercase letters in your code, because JavaScript commands and names are case-sensitive

Page 45: Introducing JavaScript. Server-Side and Client-Side Programming Server-side programming –Program placed on server that hosts Web site –Program then used

Tips for Writing Good JavaScript Code

• Add comments to your code to document the purpose of each script

• Initialize all variables at the top of your script and insert comments describing the purpose and nature of your variables

• Create customized functions that can be reused in different scripts. Place your customized functions in external files to make them available to your entire Web site