PDF Archive

Easily share your PDF documents with your contacts, on the Web and Social Networks.

Share a file Manage my documents Convert Recover PDF Search Help Contact

ProgrammingTheWebUnit5 .pdf

Original filename: ProgrammingTheWebUnit5.pdf

This PDF 1.5 document has been generated by ILOVEPDF.COM, and has been sent on pdf-archive.com on 23/08/2015 at 15:42, from IP address 103.5.x.x. The current document download page has been viewed 350 times.
File size: 435 KB (42 pages).
Privacy: public file

Download original PDF file

Document preview

Programming the web


UNIT - 5

5.1 The Javascript execution environment
5.2 The Document Object Model
5.3 Element access in Javascript
5.4 Events and event handling
5.5 Handling events from the Body elements
5.6 Button elements
5.7 Text box and Password elements
5.8 The DOM 2 event model
5.9 The navigator object
5.10 DOM tree traversal and modification.


Programming the web


UNIT - 5

5.1 The Javascript execution environment
The following features are common to all conforming ECMAScript implementations,
unless explicitly specified otherwise.
Imperative and structured
JavaScript supports all the structured programming syntax in C (e.g., if statements, while
loops, switch statements, etc.). One partial exception is scoping: C-style block-level
scoping is not supported (instead, JavaScript has function-level scoping). JavaScript 1.7,
however, supports block-level scoping with the let keyword. Like C, JavaScript makes a
distinction between expressions and statements. One syntactic difference from C is
automatic semicolon insertion, in which the semicolons that terminate statements can be
dynamic typing
As in most scripting languages, types are associated with values, not variables.
For example, a variable x could be bound to a number, then later rebound to a
string. JavaScript supports various ways to test the type of an object, including
duck typing.[19]
object based
JavaScript is almost entirely object-based. JavaScript objects are associative
arrays, augmented with prototypes (see below). Object property names are string
keys: obj.x = 10 and obj["x"] = 10 are equivalent, the dot notation being syntactic


Programming the web


sugar. Properties and their values can be added, changed, or deleted at run-time.
Most properties of an object (and those on its prototype inheritance chain) can be
enumerated using a for...in loop. JavaScript has a small number of built-in objects
such as Function and Date.
run-time evaluation
JavaScript includes an eval function that can execute statements provided as
strings at run-time.
first-class functions
Functions are first-class; they are objects themselves. As such, they have
properties and can be passed around and interacted with like any other object.
inner functions and closures
Inner functions (functions defined within other functions) are created each time
the outer function is invoked, and variables of the outer functions for that
invocation continue to exist as long as the inner functions still exist, even after
that invocation is finished (e.g. if the inner function was returned, it still has
access to the outer function's variables) — this is the mechanism behind closures
within JavaScript.
JavaScript uses prototypes instead of classes for inheritance. It is possible to
simulate many class-based features with prototypes in JavaScript.
functions as object constructors
Functions double as object constructors along with their typical role. Prefixing a
function call with new creates a new object and calls that function with its local
this keyword bound to that object for that invocation. The constructor's prototype
property determines the object used for the new object's internal prototype.
JavaScript's built-in constructors, such as Array, also have prototypes that can be


Programming the web


functions as methods
Unlike many object-oriented languages, there is no distinction between a function
definition and a method definition. Rather, the distinction occurs during function
calling; a function can be called as a method. When a function is called as a
method of an object, the function's local this keyword is bound to that object for
that invocation.
run-time environment
JavaScript typically relies on a run-time environment (e.g. in a web browser) to
provide objects and methods by which scripts can interact with "the outside
world". In fact, it relies on the environment to provide the ability to
include/import scripts (e.g. HTML <script> elements). (This is not a language
feature per se, but it is common in most JavaScript implementations.)
variadic functions
An indefinite number of parameters can be passed to a function. The function can
access them through formal parameters and also through the local arguments
array and object literals
Like many scripting languages, arrays and objects (associative arrays in other
languages) can each be created with a succinct shortcut syntax. In fact, these
literals form the basis of the JSON data format.
regular expressions
JavaScript also supports regular expressions in a manner similar to Perl, which
provide a concise and powerful syntax for text manipulation that is more
sophisticated than the built-in string functions.


Programming the web


5.2 The Document Object Model
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. Aspects of the DOM (such as its "Elements") may be addressed and
manipulated within the syntax of the programming language in use. The public interface
of a DOM is specified in its Application Programming Interface (API).
DOM is likely to be best suited for applications where the document must be accessed
repeatedly or out of sequence order. If the application is strictly sequential and one-pass,
the SAX model is likely to be faster and use less memory. In addition, non-extractive
XML parsing models such as VTD-XML, provide a new memory-efficient option.
Web browsers
A web browser is not obliged to use DOM in order to render an HTML document.
However, the DOM is required by JavaScript scripts that wish to inspect or modify a web
page dynamically. In other words, the Document Object Model is the way JavaScript sees
its containing HTML page and browser state.
Because DOM supports navigation in any direction (e.g., parent and previous sibling) and
allows for arbitrary modifications, an implementation must at least buffer the document
that has been read so far (or some parsed form of it).
Layout engines
Web browsers rely on layout engines to parse HTML into a DOM. Some layout engines
such as Trident/MSHTML and Presto are associated primarily or exclusively with a
particular browser such as Internet Explorer and Opera respectively. Others, such as
WebKit and Gecko, are shared by a number of browsers, such as Safari, Google Chrome,


Programming the web


Firefox or Flock. The different layout engines implement the DOM standards to varying
degrees of compliance.
See also: Comparison of layout engines (Document Object Model)
DOM implementations:



Xerces is a C++ DOM implementation with Java and Perl bindings

APIs that expose DOM implementations:

JAXP (Java API for XML Processing) is an API for accessing DOM providers

Alternative non-DOM tree-based XML libraries:

VTD-XML is a Java library that offers alternative tree-based view to XML

5.3 Element access in Javascript
Javascript provides the ability for getting the value of an element on a webpage as well as
dynamically changing the content within an element.
Getting the value of an element
To get the value of an element, the getElementById method of the document object is
used. For this method to get the value of an element, that element has to have an id given
to it through the id attribute.


Programming the web


<script type="text/javascript"> function getText(){ //access the element with the id
'textOne' and get its value //assign this value to the variable theText var theText =
document.getElementById('textOne').value; alert("The text in the textbox is " + theText);
} </script> <input type="text" id="textOne" /> <input type="button" value="Get text"
onclick="getText()" />
Changing the content within an element
To change the content within an element, use the innerHTML property. Using this
property, you could replace the text in paragraphs, headings and other elements based on
several things such as a value the user enters in a textbox. For this property to change the
content within an element, that element has to have an 'id' given to it through the id
<script type="text/javascript"> function changeTheText(){ //change the innerHTML












document.getElementById('text').innerHTML = 'So is HTML!'; } </script> <p
id="text">Javascript is cool!</p> <input type='button' onclick='changeTheText()'
value='Change the text' />
You can also change the text of elements based on user input:
<script type="text/javascript"> function changeInputText(){ /* change the innerHTML
property of the element whose id is 'theText' to the value from the variable usersText
which will take the value from the element whose id is 'usersText' */ var usersText =






id="theText">Enter some text into the textbox and click the buttton</p> <input
type="text" id="usersText" /> <br /> <input type="button" onclick="changeInputText()"
value="Change the text" />


Programming the web


5.4 Events and event handling
HTML Events
Not all events are significant to a program. We can simplify situations to determine
which events are significant, and which events we can leave out. For example, a leaf
hitting the bus is an event, but is not a significant event if we wish to model the cost
effectiveness of the bus route.
As we know, HTML provides elements to allow us to create web documents. We can fill
these elements with our own data and display unique web documents. We can position
the elements with careful planning, and create aesthetic web sites.
This is the object-orientated part of web documents. DHTML, or Dynamic HTML
supplies the event-driven side of things.
To begin using DHTML, and therefore event-driven HTML, we need to look at what
events happen to our web document.
There are over 50 events in all. Some can happen to a lot of HTML elements, some only
happen to specific HTML elements.
For example, moving the mouse pointer and clicking the mouse buttons create the
following events:

Programming the web


The event happens to the object. So, if the mouse pointer is over an image, say Image1,
and we click it, we create an (object, event) pair, in this case (Image1, onclick).
If we move the mouse into a select box, Select1, we create (Select1, onmouseover), and
so on.
We need technical definitions of the events to be able to appreciate what is actually
Mouse Event



A mouse button has been pressed


The mouse has been moved


The mouse pointer has left an element


The mouse pointer has entered an element


A mouse button has been released


A mouse button has been clicked


A mouse button has been double-clicked
(clicked twice rapidly)

The idea behind this is to represent all the possibilities we may encounter in computer
events. This doesn't allow for events like the mouse is dirty, or the mouse is upside-down
- these are not relevant.
You may also notice that some actions will fire two events. Moving the mouse into or out
of an element will fire both a mousemove event and a mouseover (or mouseout) event.
Clicking a mouse button fires a mousedown, click and mouseup events.
The other user-type event is generated by the keyboard.
Keyboard Event



A key has been pressed


Related documents

unit4 web
javascript events

Related keywords