Docsity

JavaScript Part 2: DOM, Math Object, and Form Validation - Prof. David L. Tarnoff, Study notes of Computer Science

Toggle sidebar
bg1
1
JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming
CSCI 2910
Client/Server-Side Programming
Topic: JavaScript Part 2
JavaScript Part 2 – Page 2 of 35CSCI 2910 – Client/Server-Side Programming
Today’s Goals
Today’s lecture will cover:
– More objects, properties, and methods of
the DOM
– The Math object
– Introduction to form validation
JavaScript Part 2 – Page 3 of 35CSCI 2910 – Client/Server-Side Programming
Intermediate File vs. HTML Output
A sometimes difficult concept is that the output
of a JavaScript script is not output to the
browser window, but instead is output to the
“intermediate” HTML file that the browser will
interpret for display.
XHTML
file
containing
scripts
XHTML
with script
output
replacing
scripts
Document
displayed
in browser
window
Browser
processes
scripts
JavaScript Part 2 – Page 4 of 35CSCI 2910 – Client/Server-Side Programming
Intermediate File vs. HTML Output
(continued)
• Since the JavaScript output is to be interpreted
by a browser as HTML, the output must contain
tags.
• Example – Assume we want a heading level 1
with a line break in the middle:
– Wrong:
document.write("This is my \n page
title");
– Right:
document.write("<h1>This is my <br />
page title</h1>");
JavaScript Part 2 – Page 5 of 35CSCI 2910 – Client/Server-Side Programming
write vs. writeln
• There are two document object methods
used to write
– document.write(string)
– document.writeln(string)
• The only difference between the two is that
writeln appends a carriage return/linefeed
(\n) to the end of the string when printing to
the intermediate file.
JavaScript Part 2 – Page 6 of 35CSCI 2910 – Client/Server-Side Programming
Prompting as Page Loads
• Remember that scripts within the
body are executed as they are
encountered
• You can take advantage of this by
prompting the user for information as
the page loads using a function such
as window.prompt().
pf3
pf4
pf5

Partial preview of the text

Download JavaScript Part 2: DOM, Math Object, and Form Validation - Prof. David L. Tarnoff and more Study notes Computer Science in PDF only on Docsity!

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 1 of 35

CSCI 2910

Client/Server-Side Programming

Topic: JavaScript Part 2

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 2 of 35

Today’s Goals

Today’s lecture will cover:

– More objects, properties, and methods of

the DOM

– The Math object

– Introduction to form validation

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 3 of 35

Intermediate File vs. HTML Output

A sometimes difficult concept is that the output

of a JavaScript script is not output to the

browser window, but instead is output to the

“intermediate” HTML file that the browser will

interpret for display.

XHTML

file containing scripts

XHTML

with script output replacing scripts

Document displayed in browser window

Browser processes scripts

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 4 of 35

Intermediate File vs. HTML Output

(continued)

• Since the JavaScript output is to be interpreted

by a browser as HTML, the output must contain

tags.

• Example – Assume we want a heading level 1

with a line break in the middle:

  • Wrong:

document.write("This is my \n page

title");

  • Right:

document.write("This is my

page title");

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 5 of 35

write vs. writeln

• There are two document object methods

used to write

  • document.write( string )
  • document.writeln( string )

• The only difference between the two is that

writeln appends a carriage return/linefeed

(\n) to the end of the string when printing to

the intermediate file.

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 6 of 35

Prompting as Page Loads

• Remember that scripts within the

body are executed as they are

encountered

• You can take advantage of this by

prompting the user for information as

the page loads using a function such

as window.prompt().

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 7 of 35

Prompting as Page Loads

(continued)

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 8 of 35

Double vs. Single Quotes

  • As with any language that relies heavily on the use

of output strings, we must have a way to identify

quotation marks within a string without affecting

the way the interpreter views the string.

  • In JavaScript, there are three ways to embed

quotation marks within a string:

  • use single quotes within a string identified using double quotes
  • use double quotes within a string identified using single quotes
  • use the JavaScript escape characters ' or "

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 9 of 35

Double vs. Single Quotes (cont.)

Examples:

  • document.write("");
  • document.write('');
  • document.write("");

All three methods should work regardless of

browser

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 10 of 35

Declaring Variables

  • Variables are declared using the keyword var
  • Example: var int_value, string_value
  • When variables are declared, they are not

assigned a default value, unless specified by the

programmer

  • All variables in JavaScript can contain a value of

any data type, i.e., JavaScript does not rigorously

follow types an will try to convert between types

  • null is a valid variable value

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 11 of 35

Parsing Functions

• parseInt(string, radix) -- returns the first integer

in the string. The radix argument specifies the

base in which the number is represented in the

string, e.g., 16 (hexadecimal), 10 (decimal), or 2

(binary).

• Example:

parseInt("313 Gilbreath", 10);

would return 313

• If the first character is not a number, then the

function returns "NaN" indicating the value is

not a number.

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 12 of 35

Parsing Functions (continued)

• parseFloat(string) – returns the first floating

point number in the string.

• Example:

parseFloat("2.98% of students");

would return 2.

• If the first character is not a number, then

the function returns "NaN" indicating the

value is not a number. This includes

characters such as $ or #.

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 19 of 35

Math Object Methods (continued)

  • Math.floor(x) – returns the largest integer less than or equal to x
  • Math.log(x) – returns the natural logarithm of x
  • Math.max(x, y) – returns the greater of x and y
  • Math.min(x, y) – returns the lesser of x and y
  • Math.pow(x, y) – returns the value of xy
  • Math.random() – returns a pseudo-random number between 0 and 1
  • Math.round(x) – rounds x to the nearest integer
  • Math.sin(x) – returns the sine of x where x is in radians
  • Math.sqrt(x) – returns the square root x
  • Math.tan(x) – returns the tangent of x where x is in radians

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 20 of 35

Accessing Data from Forms

  • Before we get to the point where we're trying to

access data, let's talk a little about the form object

and its properties and methods

  • One way to "point" to a specific form object is to

access the document object forms array.

document.forms[n]

  • The most reliable way to reference a form object is

to consistently identify everything with the name

and id attributes.

document. formname

document.forms[" formname "]

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 21 of 35

Form Object Properties

  • action – Returns the URL address to which the form's data will be submitted.
  • length – Returns the number of elements in the form.
  • method – Returns a string specifying data submission method, i.e., either 'get' or 'post'.
  • target – Returns the target window where the form's response will appear.

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 22 of 35

Form Object Methods

  • reset( ) – Resets the form to its default values. (Same result as clicking the reset button.)
  • submit( ) – Submits the form's data. (Same result as clicking the submit button.)

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 23 of 35

Accessing Element Values

  • One way to "point" to a specific element in a form is to

access the element array under the form object.

document.formname.elements[n]

where n equals the position the element holds in the

order that the elements were added to the form. Huh?

  • The most reliable way to reference an element of a

form is to consistently identify everything with the

name and id attributes.

document. formname.elementname

document.forms[" formname "]. elementname

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 24 of 35

Form Element Object Properties

  • defaultValue – sets or returns a string representing the default value of the element.
  • name – sets or returns the element's name or id attribute.
  • type – returns the element's type property.
  • value – sets or returns the element's value attribute. Works differently for different elements.

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 25 of 35

Form Element Object Methods

  • blur( ) – removes the focus from the specified element
  • click( ) – simulates a mouse-click for some elements
  • focus( ) – returns focus to the specified element

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 26 of 35

The Use of value

Checkbox First Second Third

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 27 of 35

The Use of value (continued)

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 28 of 35

The Use of value (continued)

var output_string; function printVals() { output_string="Checkbox value = " + document.userinput.gen_check.checked + "\n"

  • "Radio button value = " + document.userinput.gen_radiobutton.value + "\n"
  • "Text value = " + document.userinput.gen_text.value + "\n"
  • "Select value = " + document.userinput.gen_select.value + "\n"
  • "File selection value = " + document.userinput.gen_file.value + "\n"
  • "Button value = " + document.userinput.gen_button.value + "\n"; document.userinput.output.value=output_string; }

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 29 of 35

The Use of value (continued)

CSCI 2910 – Client/Server-Side Programming JavaScript Part 2 – Page 30 of 35

The Use of value (continued)

  • Some of the values make sense, e.g., the text value equaled the text in the box.
  • Some of the values did not make sense
    • Checkbox value always equals "on"
    • Radio buttons always equal "undefined"
    • Button value equals the text on the button