JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of...

63
JavaScript JavaScript and HTML Text Chapter 4
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    249
  • download

    2

Transcript of JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of...

Page 1: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

JavaScript

JavaScript and HTML

Text Chapter 4

Page 2: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Client-side JavaScript

• Defines a collection of objects, methods and properties that allow scripts to interact with XHTML.

• This chapter covers DOM, although an understanding of DOM is not necessary to use client-side JavaScript.

• It covers techniques for accessing document elements, and the basics of events and event-handling.

Page 3: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM

• The Document Object Model (DOM) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents.[1] Objects in the DOM tree may be addressed and manipulated by using methods on the objects. The public interface of a DOM is specified in its application programming interface (API). The history of the Document Object Model is intertwined with the history of the "browser wars" of the late 1990s between Netscape Navigator and Microsoft Internet Explorer, as well as with that of JavaScript and JScript, the first scripting languages to be widely implemented in the layout engines of web browsers.

Page 4: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The execution environment• A browser displays an XHTML document in a window on

the computer screen. This is the JavaScript Window object.

• All variables in your script are properties of some object. The Window’s properties are visible to all scripts in the displayed document, (this includes global variables).

• There can be multiple Window objects.• The Document object is the displayed HTML document.

It is a property (the document property) of a Window object.

• The Document object has a Forms array. Each Forms array element has an elements array as a property.

• The Document object also has a property arrays for anchors, links, images and applets.

Page 5: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Hierarchy

Window Object

Document property Other window properties

Forms Array

A Form in the array

Elements array

Another form…etc

Page 6: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM: Document Object Model

• Under development since mid-90s.• Currently DOM 3 is the latest version• DOM1 focused on the XHTML and XML

document model.• DOM 2 includes document traversal and

an event model.

Page 7: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Values indicate the level of support in the most recent version of the layout engine, or (if a version number is given) in the specified version.

Value Meaning

YesIndicates that the layout engine fully supports this property/element when valid values are used.

NoIndicates that the property/element is completely ignored.

Partial

Indicates that the property/element is understood, but that not all values are supported. Supported values are implemented correctly.

IncorrectIndicates that the property/element is understood, but that it is not implemented correctly in all cases.

Experimental

Indicates that the property/element is understood, but supported under an alternate name. May be incomplete or buggy.

DroppedIndicates that the property/element is no longer supported.

Nightly build

Indicates that the property/element is supported to some extent in an experimental/nightly build. Future support is expected.

DependsIndicates that the property/element is supported only on certain platforms, or if certain settings are configured.

Page 8: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM compliance by layout engines

TridentTasman

Gecko WebKit KHTML Presto

DOM1 6.0 Yes 1.0 85 Yes 1.0

DOM2 Mostly Partial Mostly Partial Mostly Mostly

DOM3 No No Partial Partial Partial Partial

Page 9: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The document object<html > <head><title> onLoad event handler </title> <script type = "text/javascript">// The onload event handler function load_greeting () { alert("You are visiting the home page of \n" + "Pete's Pickled Peppers \n" + "WELCOME!!!"); } </script> </head> <body onload="load_greeting();">Some text <p /> </body></html>

Page 10: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The previous html rendered

Page 11: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The document object

document

headbody

title script

function

Some text

Page 12: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM support in JavaScript

• Elements of XHTML are objects in JavaScript, their attributes are object properties. Example,

<input type=“text” name=“address”>

• type and name are properties of the input object

Page 13: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Accessing objects and properties

• Addresses are needed to access objects and properties of the document from within a script.

• The address is referred to as the object’s DOM address.

Page 14: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Text example

<?xml version = "1.0" encoding = "utf-8"?><!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns = "http://www.w3.org/1999/xhtml"> <head><title> address example </title> </head> <form name ="myform" action=""> <input type="button" name="turniton"> </form> </body></html>

Page 15: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Address of the button is…?

• Using the DOM address, the button is document.forms[0].elements[0]

• Using element names, the address of the button is document.myForm.turniton

• A validation issue: the XHTML 1.1 standard does not allow the use of the name attribute for a form, though the browser has no trouble with it.

• You can also use a JavaScript method, getElementById(“…”) as in

document. getElementById(“turniton”) • Any expression that evaluates to string can serve as the

parameter to getElementById

Page 16: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Names and ids

• Names are needed for form-processing and ids are handy for DOM addressing so often both are used.

• Buttons in a group of radiobuttons always have the same name. Buttons in a group of checkboxes may have the same name. In such cases, the names cannot be used in the DOM address. They may have an id though.

• Implicit arrays are associated with button groups, and these provide an alternative to using names or ids for these addresses. (See course ppt example).

Page 17: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Table 5.2 excerpt: Text shows event attributes and their tags

Attribute Tag description

onblur <a> Link loses input focus

<button> Button loses input focus

onchange <input> Input element is changed and loses input focus

onclick <a> Link acquires input focus

Page 18: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

How to register the event handler with DOM 0 event model

• Assign the event handler script to an event tag attribute, eg.,

<input type =“button” name=“whocares” onclick=“alert(‘you clicked me’);”/>

• If the handler is more than one statement, use a function, as in,

<input type =“button” name=“whocares” onclick=“myButtonHandler();”/>

Page 19: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Body elements

• load and unload are the two most common body-element events.

• See text example load greeting in previous slide

Page 20: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Buttons

• Buttons are a good way to get input from a browser. click is the most common button event

• Example from previous slide shows how to register an event to handle button-click:

<input type =“button” name=“whocares” id=“somebutton” onclick=“myButtonHandler();”/>

Page 21: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Buttons

• Another way to do it would be to assign the handler to the associated event property on the button:

document.getElementById(“somebutton”).onclick= myButtonHandler;

Page 22: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Rb example: form in body<form id = "myForm" action = ""> <p> <input type = "radio" name = "planeButton" onclick = "planeChoice(152)" /> Model 152 <br /> <input type = "radio" name = "planeButton" onclick = "planeChoice(172)" /> Model 172 (Skyhawk) <br /> <input type = "radio" name = "planeButton" onclick = "planeChoice(182)" /> Model 182 (Skylane) <br /> <input type = "radio" name = "planeButton" onclick = "planeChoice(210)" /> Model 210 (Centurian) </p></form>

Page 23: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Slightly modified Radiobutton click event handlerfunction planeChoice (plane) { var plane;var s; switch (plane) { case 152: s="A small two-place-airplane for flight training"; break; case 172: s="The smaller of two four-place airplanes"; break; case 182: s="The larger of two four-place airplanes"; break; case 210: s="A six-place high-performance airplane"; break; default: s="Error in JavaScript function planeChoice"; } alert(s);}

Page 24: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The form

Page 25: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Alert from rb click

Page 26: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Different version of this rb example <body> <h4> Cessna single-engine airplane descriptions </h4> <form id = "myForm" action = "handler"> <p> <input type = "radio" name = "planeButton" value = "152" /> Model 152 <br /><input type = "radio" name = "planeButton" value = "172" /> Model 172 (Skyhawk) <br /> <input type = "radio" name = "planeButton" value = "182" /> Model 182 (Skylane) <br /> <input type = "radio" name = "planeButton" value = "210" /> Model 210 (Centurian) </p> </form> <script type = "text/javascript"> var dom = document.getElementById("myForm"); dom.elements[0].onclick = planeChoice; dom.elements[1].onclick = planeChoice; dom.elements[2].onclick = planeChoice; dom.elements[3].onclick = planeChoice; </script>

Page 27: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Different version of the same from text function planeChoice (plane) {var dom = document.getElementById("myForm");for (var index = 0; index < dom.planeButton.length; index++) { if (dom.planeButton[index].checked) { plane = dom.planeButton[index].value; break; } }switch (plane) { case "152": alert("A small two-place airplane for flight training"); break; case "172": alert("The smaller of two four-place airplanes"); break; case "182": alert("The larger of two four-place airplanes"); break; case "210": alert("A six-place high-performance airplane"); break; default:: alert("Error in JavaScript function planeChoice"); break; } }

Page 28: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Text box events

• Textbox creates four events: blur, focus, change, select.

Page 29: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

An example from the text

• Text example (pg 207 nochange.html and nochange.js)

• I saved this in

• …\Higgindm\source\chapt5\coffee.html and coffee.js

Page 30: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

The Compute cost function

function computeCost() { var french = document.getElementById("french").value; var hazlenut = document.getElementById("hazlenut").value; var columbian = document.getElementById("columbian").value;

// Compute the cost

document.getElementById("cost").value = totalCost = french * 3.49 + hazlenut * 3.95 + columbian * 4.59; } //* end of computeCost

Page 31: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

A piece of the table to create the form

<tr> <th> Product Name </th> <th> Price </th> <th> Quantity </th> </tr>

<!-- Now, the table data entries -->

<tr> <th> French Vanilla (1 lb.) </th> <td> $3.49 </td> <td> <input type = "text" id = "french" size ="2" /> </td> </tr> <tr>

Page 32: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

computation of the total cost<p>

<input type = "button" value = "Total Cost"

onclick = "computeCost();" />

<input type = "text" size = "5" id = "cost"

onfocus = "this.blur();" />

</p>

Page 33: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Ordering coffee: user can’t alter any cost or price textbox

Page 34: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Validating form input

• Format, completeness (including datatypes, etc) is commonly done in JavaScript, shifting this work off the server. Besides the fact that the client usually has a lighter load, doing this data entry checking on the client saves network traffic.

• An alert indicating the error, putting the field in focus, and selecting the contents would be appropriate actions. The focus() method and select() methods on the DOM element would effect the latter two actions.

Page 35: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

IE6 vs Moz7

• In IE6 focus and select only work if the handler is registered by assigning an event property. In Moz7 select works ok but focus does not.

Page 36: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Return value of data checking handler

• A handler checking form data should return false to keep the (bad) form data from being sent on to the server.Return true if the data is ok.

• The text provides an example: the user enters a password, then enters it again. We need to check that the pw was entered into the first field (it is not the empty string) and that the contents of the first pw field match the second pw field.

• If all is well the checker returns true. Otherwise it returns to the first pw field.

• The checker is called on submit, or when the second textbox loses focus, the onblur event.

Page 37: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Entering two different pws in mozilla

Page 38: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

After clicking ok in the alert in IE: note focus

Page 39: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Function to check pwsfunction chkPasswords() { var init = document.getElementById("initial"); var sec = document.getElementById("second"); if (init.value == "") { alert("You did not enter a password \n" + "Please enter one now"); init.focus(); return false; } if (init.value != sec.value) { alert("The two passwords you entered are not the same \n" + "Please re-enter both now"); init.focus(); init.select(); return false; } else return true;}

Page 40: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Body & form<body><h3> Password Input </h3><form action = ""> <p> Your password <input type = "password" id = "initial" size = "10" /> <br /><br /> Verify password <input type = "password" id = "second" size = "10" /> <br /><br /> <input type = "reset" name = "reset" /> <input type = "submit" name = "submit" /> </p></form><script type = "text/javascript"><!--// Set submit button onsubmit property to the event handler document.forms[0].onsubmit = chkPasswords; // --></script></body>

Page 41: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Validator example in text

• Check a name and phone number for format errors.

Page 42: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Input validation: check name

Page 43: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Checking input: phone number

Page 44: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

This input was accepted…. Is it ok?

Page 45: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Check namefunction chkName() { var myName = document.getElementById("custName");// Test the format of the input name // Allow the spaces after the commas to be optional// Allow the period after the initial to be optional var pos = myName.value.search(/\w+, ?\w+, ?\w.?/); if (pos != 0) { alert("The name you entered (" + myName.value + ") is not in the correct form. \n" + "The correct form is: " + "last-name, first-name, middle-initial \n" + "Please go back and fix your name"); myName.focus(); myName.select(); return false; } else return true;}

Page 46: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Check number

function chkPhone() { var myPhone = document.getElementById("phone");// Test the format of the input phone number var pos = myPhone.value.search(/^\d{3}-\d{3}-\d{4}$/); if (pos != 0) { alert("The phone number you entered (" + myPhone.value + ") is not in the correct form. \n" + "The correct form is: ddd-ddd-dddd \n" + "Please go back and fix your phone number"); myPhone.focus(); myPhone.select(); return false; } else return true;}

Page 47: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Form and script<form action = ""> <p> <input type = "text" id = "custName" /> Name (last name, first name, middle initial) <br /><br /> <input type = "text" id = "phone" /> Phone number (ddd-ddd-dddd) <br /><br /> <input type = "reset" id = "reset" /> <input type = "submit" id = "submit" /> </p></form><script type = "text/javascript"><!--// Set form element object properties to their // corresponding event handler functionsdocument.getElementById("custName").onchange = chkName;document.getElementById("phone").onchange = chkPhone;// --></script>

Page 48: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Validator2 from text

• Uses the DOM2 model.

• Documents can mix the Dom0 and DOM2 models.

• But DOM2 is not a superset of DOM0 and doesn’t support its model.

Page 49: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

the DOM2 model

Module Event interface Event types

HTMLEvents Event abort, blur, change, error, focus, load,…

MouseEvents MouseEvent click,mousedown,

mousemove,

mouseup, mouseover,…

Page 50: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM2 event-handling

• much more complicated than DOM0: There are 3 phases: capture, target node execution, and bubbling

• In capture, the event object propagates down from the root to the target node looking for an enabled event-handler.

• When the event reaches the target node, any registered handler there are executed.

• In bubbling, the event travels back to the root executing enabled event-handlers for this event.

Page 51: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

bubbling

• Not every event bubbles. • load and unload events do not bubble. • Mouseevents do bubble.• Any handler can stop further propagation using

the stopPropagation method of the event.• Bubbling came from exceptionhandling.

Exceptions in C++ and Java can be thrown and caught, so to avoid redundancy a single handler might deal with all events of a certain type.

Page 52: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM0 vs DOM2 more…

• DOM0 validating methods return false to prevent sending a form to a server.

• DOM2 has a method preventDefault which serves the same purpose.

• We saw how to register a method to handle an event in DOM0. In DOM2 it is effected by using the addEventListener method.

Page 53: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM0 vs DOM2 more…

• addEventListener takes 3 parameters, the name of the event as a string literal (eg., “mouseup”), the handler function specified as code or a function name, and finally a boolean indicates if the function is enabled for the capture phase. If not, it may be called at the target node or during bubbling.

• removeEventListener – a method which takes the same 3 parameters as addEventListener – makes it possible to remove an event handler.

Page 54: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

DOM0, Moz7 and DOM2

• DOM0 and Moz7 treat the keyword this in an event handler as a reference to the target node.

• This is not required in DOM2 and other browsers may not support it so use of this in a handler may be non-portable.

• The alternative is to use the currentTarget property of Event which always references the object on which the handler is being executed.

Page 55: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Mouse

• MouseEvent inherits from Event interface. It adds several properties, most useful of which are clientX and clientY, the x and y coordinates of the mouse event in the browser window, measured from upper LH corner of the document in the browser window (not the upper left of the display).

Page 56: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Redo of validator in textfunction chkName(event) {// Get the target node of the event var myName = event.currentTarget;var pos = myName.value.search(/\w+, ?\w+, ?\w.?/); if (pos != 0) { alert("The name you entered (" + myName.value + ") is not in the correct form. \n" + "The correct form is: " + "last-name, first-name, middle-initial \n" + "Please go back and fix your name"); myName.focus(); myName.select(); } }

Page 57: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

validator2 in textfunction chkPhone(event) {// Get the target node of the event var myPhone = event.currentTarget;// Test the format of the input phone number var pos = myPhone.value.search(/^\d{3}-\d{3}-\d{4}$/); if (pos != 0) { alert("The phone number you entered (" + myPhone.value + ") is not in the correct form. \n" + "The correct form is: ddd-ddd-dddd \n" + "Please go back and fix your phone number"); myPhone.focus(); myPhone.select(); } }

Page 58: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Body & form<body><h3> Customer Information </h3><form action = ""> <p> <input type = "text" id = "custName" /> Name (last name, first name, middle initial) <br /><br /> <input type = "text" id = "phone" /> Phone number (ddd-ddd-dddd) <br /><br /> <input type = "reset" /> <input type = "submit" /> </p></form><script type = "text/javascript"><!--/* Get the DOM addresses of the elements and register the event handlers */var customerNode = document.getElementById("custName");var phoneNode = document.getElementById("phone");customerNode.addEventListener("change", chkName, false);phoneNode.addEventListener("change", chkPhone, false);// --></script></body>

Page 59: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

You can get the browser name

• appName of navigator returns the browser.

Page 60: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Navigate.html in text

<html><head><title> Using navigator </title><script type = "text/javascript"><!--// The event handler function to display the browser name// and its version numberfunction navProperties() { alert("The browser is: " + navigator.appName + "\n" + "The version number is: " + navigator.appVersion + "\n");}// --></script></head><body onload = "navProperties()"></body></html>

Page 61: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Mouse click coordinates<html> <head> <title> Drag and drop </title> <script type = "text/javascript"><!-- function mouseFunction(event) { var posX = event.clientX; var posY = event.clientY;alert("position x="+ posX+ " y="+posY); event.stopPropagation(); event.preventDefault();} //** end of grabber// --> </script> </head> <body> <img id = "Patricia" src = "Patricia.jpg" alt = "(Picture of Patricia)" onmousedown = "mouseFunction(event);"/> </body></html>

Page 62: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

Picture comes up

Page 63: JavaScript JavaScript and HTML Text Chapter 4. Client-side JavaScript Defines a collection of objects, methods and properties that allow scripts to interact.

On mousedown alert