91

Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Embed Size (px)

Citation preview

Page 1: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document
Page 2: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Creating Active Web Pages

Chapter 8

Page 3: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Learn how to…

• List the primary scripting languages and describe JavaScript.

• Understand the purpose of the Document Object Model (DOM).

• Use cookies.• Use cascading style sheets.• Use Dynamic HTML to create animated

Web pages.• Understand XML, XSL, XSLT, XHTML,

and SMIL.

Page 4: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Introduction to Scripting

Page 5: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Scripting

• Scripting is the act of writing little computer programs that can enhance the appearance and functionality of a Web page.

Page 6: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Scripting Languages

• JavaScript runs client-side in the browser without requiring any server-side processing.

• Server-side scripting languages include:– VBScript and JScript– Microsoft’s Active Server Page (ASP) languages– C# (pronounced C-sharp)– Java– J# (Microsoft’s version of Java – pronounced J-sharp)

Page 7: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Where Do Scripts Go?

• You can put JavaScript in the head or in the body section of a Web page.

• Scripts can also reside in a separate file called an include file, which gets included in the page at runtime.

• A function is a named procedure you can call upon by name any time you need to execute the code in the function.

Page 8: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Where Do Scripts Run?

• Scripts run either on the client (or browser) or on the server that hosts the Web site.– JavaScript runs on the client.

• Use whenever the process you are handling can be accomplished by the browser without requiring any programming on the server side.

– ASP scripts run on the server.

Page 9: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Hello World!

Page 10: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

• The <script> start and </script> stop tags mark the beginning and ending of a script on a Web page.

• The language attribute defines the language as JavaScript.

• The document object is one of the JavaScript objects.• The write() method causes a string of characters to

be written onscreen.

Page 11: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Variables and Strings

• A variable is a place in the computer’s RAM that remembers, or stores, the value of something changeable.

• A string is a sequence of one or more alphanumeric characters.

• A string variable is a place in the computer memory that remembers, or stores, the alphanumeric characters in a string.

Page 12: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Variables

• A numeric variable is a place in the computer memory that remembers, or stores, a number point.

• A floating point number has a decimal point with one or more numbers after the decimal point.

Page 13: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Variable Names

• A variable name is the identifier used to refer to, or call upon, a place in the computer memory that stores the value of a variable.

• Follow a naming convention whereby integers begin with the letter i, strings begin with the letter s, and floating point numbers begin with the letter f.

Page 14: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Operators

• An operator is a symbol that causes a script to perform some kind of action on a variable or value.

• The assignment operator assigns values to variables.

Page 15: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Example

• Comment statements begin with the special symbol //.

Page 16: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

Page 17: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Concatenating Strings

• To concatenate means to join strings together via the concatenation operator.– In JavaScript, the concatenation operator is

the + sign.

Page 18: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Example

Page 19: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Numeric Variables

Page 20: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Displaying Strings

The <strong> and </strong> tags will make the person’s age and weight appear bold.

JavaScript uses the special symbol " \" to denote a quote sign inside a string. The code to display the string Santa sang "Jingle Bells" as he drove the sleigh is given below.

Page 21: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Objects and Methods

• An object is a self-contained entity consisting of properties and methods enabling you to do something programmatically.

• A method is a little computer program built into an object to enable the object to do something.

Page 22: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Properties

• A property is an attribute of an object that has a value.– Properties can be used to check whether a

user typed in his e-mail address properly.

Page 23: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Events

• An event is an action that provides an opportunity to trigger a script that can use objects to inspect properties and execute methods that make things happen onscreen or keep records behind the scenes.

Page 24: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Most Common Events

• Mouseover

• Mouse click

• Double-click

• Page load

Page 25: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

JavaScript Clock Project

Page 26: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Customizing Date and Time

Page 27: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Example

Page 28: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Document Object Model(DOM)

Page 29: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOM

• The document object model (DOM) is the official W3C structural definition of the objects, methods, and properties that comprise documents on the World Wide Web.– Visit www.w3.org/DOM for more information.

Page 30: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOM Objects

Page 31: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOM Objects

Page 32: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Intrinsic Events

Page 33: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Dot Notation

Page 34: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Title Bar Clock Example

This script will display the current time in the title bar.

Page 35: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Title Bar Clock Example

Page 36: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Arrays

• An array is a named table of memory locations in which values can be stored.

– Defines an array with 7 slots (0 through 6).

– Assigns values to the array slots.

Page 37: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOM Objects by Arrays

• When a Web page loads, the browser creates arrays for the images, forms, links, anchors, and all the other elements onscreen. As the browser encounters objects on the page, it places them into these arrays. The arrays are indexed sequentially, beginning with zero.– For example, the first image on the page goes into the

images array slot 0 referred to as document.images[0]– In this example, the third button on the first form of a

Web page would be referred to as document.forms[0].elements[2]

Page 38: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOM Objects by Name

• Use the name and id attributes to give the element a unique identifier.– Older versions of HTML use name.– New versions of HTML use id.

Page 39: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Example

The following script provides three buttons that allow users to make an image smaller or larger.

Page 40: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Alert Boxes

• To aid in troubleshooting, insert an alert box at the point in the script at which you want to inspect the value of the variable.

• An alert box is a window that a script creates by executing the alert() method of the JavaScript window object.– Insert the string of characters and variables you want

displayed between the ( ) in the alert() method.

Page 41: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Inspecting a Variable

• The code to inspect the value of a variable using an alert box to troubleshoot is given below:

Page 42: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

JavaScript Code Sources

Page 43: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Rollover Effects

• The following script causes the photo to change when you rollover it with the mouse:

Page 44: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Rollover Effects

• The following script causes the picture to change from a dimly colored photo to full color in Internet Explorer:

Page 45: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Maintaining State in Cookies

Page 46: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Cookies

• A cookie is a place in the computer’s memory where browsers can store information about the user.– persistent cookies are stored in small text files

on the user’s hard disk.– per-session cookies are stored in RAM.

• Cookies can be used to keep track of what you do on Web sites – that is, to maintain state.

Page 47: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Reading and Writing Cookies

Page 48: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

• To make the cookie persistent for 1 minute:

Page 49: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

• The script stores the click counter value in a cookie called ClickCounter.

• The value is read into a variable called iClickCounter.

Page 50: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

Page 51: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Cascading Style Sheets

Page 52: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Cascading Style Sheets

• A cascading style sheet (CSS) is a set of rules that define styles to be applied to entire Web pages or individual Web page elements.– Each rule consists of a selector followed by a

set of curly braces containing the style properties and their values.

Page 53: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Cascading Style Sheets

Page 54: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Types of CSS

• An external CSS keeps all the style definitions in a separate CSS file that you include in a Web page at runtime by using the <link> tag to apply the style sheet to the page.

• An embedded CSS is a style sheet that gets copied physically into the head of the Web page and applies to the Web page as a whole.

• An inline CSS is a style sheet that applies to only one page element so it gets copied “inline” on the page with that element.

Page 55: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

An Inline CSS

Page 56: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

An Embedded CSS

• The embedded CSS goes into the head section of the page, and the style rules defined there apply to the whole page. Below, styles are assigned to h1 headers and paragraphs:

Page 57: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Inline and Embedded CSS

Page 58: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

An External CSS

• Create a style sheet and save it with the .css extension:

In the <head> section, reference your CSS:

Page 59: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

<span> and <div> Tags

• Use the <span> and </span> tags to stylize part, instead of all, of a Web page element. In this example, “yellow words” has its own style:– <p>Notice how <span style="color: yellow">yellow

words</span> appear onscreen.</p>

• The <div> and </div> tags create a new division or line break, but otherwise function just like the <span> tag.

Page 60: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Creating a Style Class

• A class is a named definition of one or more styles.– Create the class by prefixing its name with a

dot in the CSS file.

Page 61: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Creating a Style ID

• An ID is a unique style identifier intended to apply to one, and only one, Web page element onscreen.

Page 62: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Absolute Positioning

• Absolute positioning enables you to position page elements precisely onscreen based on x,y coordinates.– The upper-left corner of the browser window

is position 0,0.

– Absolute positioning allows you to position overlapping objects. The z-index can be used to tell the browser the order in which to display objects that overlap.

Page 63: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Z-index Example

Page 64: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Z-index Example

Page 65: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Dynamic HTML

Page 66: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Dynamic HTML

• Dynamic HTML is a term invented by Microsoft to refer to the animated Web pages you can create using the DOM to combine HTML with style sheets and scripts that bring Web pages to life.

Page 67: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Animation Effects

• You can use absolute positioning to place an object anywhere on the screen.

• JavaScript has a timer event that allows you to vary the x,y coordinates dynamically.

Page 68: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Animation Effects Code

Page 69: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

• The <body> start tag is programmed to fire the BeginAnimation() function when the page loads:

Page 70: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Code Analysis

Page 71: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Gradient Effects

• A gradient is a graphical effect created by colors fading gradually across or down the screen.

Page 72: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Dynamic Gradient Effects

• The code to create a gradient background for a Web page is given below:

– The color is formatted #AARRGGBB, where AA is the opacity ranging from 00 (transparent) to FF (full color), RR is red, GG is green, and BB is blue.

Page 73: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Page Transitions

• A page transition is the style or manner in which the screen changes when the browser brings up a new document and displays it onscreen.

Page 74: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Page Transitions

• To set the transition effect that users will see when the page comes onscreen, insert the following in the <head> section:

• To set the transition effect that users will see when the page leaves the screen:

Page 75: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Page Transitions

Page 76: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Microsoft’s Code Generator

http://msdn.microsoft.com/workshop/samples/author/dhtml/DXTidemo/DXTidemo.htm

Page 77: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XML and XHTML

Page 78: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

What Is XML?

• XML (eXtensible Markup Language) is a simple, self-describing markup language that enables computers to read and understand the structure of different kinds of documents and to exchange data across different operating systems, software applications, and hardware configurations without requiring any human intervention.– Tags define the structure of the data.– Visit www.xml.org for more information.

Page 79: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XML Schema

• An XML schema is the structural definition of the types of elements that can appear in a document, the attributes each element may have, and the relationships among the elements.– Schema files end in XSD (XML Schema

Definition).

Page 80: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Sample XML Schema

Page 81: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Encoding XML Data

Page 82: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

DOCTYPE Declaration

• The DOCTYPE declaration identifies the schema that defines the tag structure.– If all tags precisely follow the schema, the

document validates and is well formed.– If it does not validate, it is malformed.

Page 83: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XML Editor

• XML files are plain text and can be created and edited with Notepad.

• However, it is easier to use an XML editor.– Microsoft’s Visual Studio .NET is the premier

suite of tools for creating Web applications.

Page 84: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XSL

• eXtensible Stylesheet Language (XSL) is an XML dialect that Web designers use to specify the styling, layout, and pagination of the structured content in an XML document for some targeted presentation medium, such as a Web browser, a printer, an eBook, a screen reader, or a hand-held device.– Visit www.w3.org/TR/xsl for more information.

Page 85: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XSLT

• XSL Transformation (XSLT) language is an XML dialect that Web designers use to transform documents from one format into another.

Page 86: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XSLT Example

Page 87: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Loose and Strict HTML

• Loose structural rules means that the Web page is using structural elements in use today but that may fade in the future.– To declare a page using the loose definition:

– To declare a page using the strict definition:

Page 88: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

Loose and Strict XHTML

• XHTML is a reformulation of HTML in XML.• Loose XHTML relates to the differences

between XML and SGML.– To define loose XHTML:

– To define strict XHTML:

• For more info, go to www.w3.org/TR/xhtml1

Page 89: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XML Module and SMIL

• An XML module is a collection of semantically- related XML elements and attributes oriented toward accomplishing a certain task or function.

• Synchronized Multimedia Implementation Language (SMIL) is an XML-based language that was created by the W3C for enabling developers to include multimedia events in Web documents.

Page 90: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document

XHTML+SMIL

• XHTML+SMIL permits the Web designer to use SMIL animations, timings, and transitions within a conventional HTML or CSS page layout.– HTML+TIME is Microsoft’s implementation

that works with IE versions 5.5 and later.

Page 91: Creating Active Web Pages Chapter 8 Learn how to… List the primary scripting languages and describe JavaScript. Understand the purpose of the Document