Web Engineering Javascript (Part 2) : Muhammad Umair Naru Department of Computer Science
Web Engineering Javascript (Part 2) : Muhammad Umair Naru Department of Computer Science
JavaScript (Part 2)
Muhammad Umair Naru
Department of Computer Science
Now we get into the cool stuff
• window.history
– length reflects the number of entries in the history list
– history.go(-1)
– history.back()
08/25/20 Introduction to Javascript 5
Form Object
• Form objects can be accessed by:
– window.document.myForm OR
window.document.forms[0]
• Properties
– action, target, length, method, etc…
• Functions
– window.document.myForm.submit();
– window.document.myForm.reset();
• Accessing Form Field Values
– window.document.myForm.firstname.value
HTML Document
Code is written as a function
Header with which may in turn act on
Javascript code element
Body with
Elements
Text Box
Document element requests
function activation for a given
action – onclick
What Javascript Is and Is Not
• JavaScript is
– an interpreted loosely-typed object-based(?)
language
– event driven, embedded into HTML, and
dependent upon a simplified DOM
– still evolving and far from platform independent
• JavaScript is not
– simplified Java -- the two languages have
disjoint sets of capabilities
– simple -- mastery of JavaScript requires
advanced programming skills
What JavaScript Can and Can’t Do
• JavaScript can:
– Control document appearance and content,
including images
– Control the browser & interact with the user
– Establish network connections
– Read and write client state with cookies
• JavaScript can’t:
– Read or write files on the client
– Read or write files on the server without help
from a server-side script
– Support any kind of multithreading
JavaScript Basics
JavaScript Syntax
• Like Java, JavaScript…
– Is case-sensitive
– Ignores whitespace between “tokens”
– Uses C++ style (i.e. //) & C – style (i.e. /* */) comments
– Method and variable names must begin with a letter or _
• Unlike Java, Javascript…
– Semi-colons are optional.
– JavaScript is an untyped language
– Does not support data hiding (private, public, or protected)
– Variable declaration “var” is only required for “local” variables inside a
function when the same variable is used as a “global” variable.
Data Types
• Primitive Data Types
– Integer, Float, String, Boolean (true/false only)
– Functions are code that may be executed multiple times
– Objects are named pieces of data that have a collection of
properties
– Arrays are indexed collection of data values
– Null indicates “no value”
– Undefined returned when a variable doesn’t exist
• JavaScript does allow you to create your own
“classes” (more on this later.)
More on Strings
• A series of characters enclosed in single or
double quotes.
s = “Hello, ” + “world”; // Hello, world
// Object arrays
for (prop in myObject) {
document.write(“name: “ + prop ”; value: “ +
myObject[prop], “<br>“); }
Client-Side Program Structure
Client-Side Program Structure
• It is preferable to put JavaScript in an external file.
<script type="text/javascript” src="valid.js” />
• A single HTML file may contain more than one pair of (non-
overlapping) <SCRIPT> tag pairs.
– JavaScript statement tags are executed in the order they appear.
– Javascript functions are executed when invoked
setTitle: function(t) {
this.title = t;
}
};
function setUserName( ) {
var n = document.getElementById(“firstName”).value;
return n;
}
Pre-built Objects
browser
window navigator
<head> <body>
<title>
<a> <p>
text
Attr: href text text
Document Object Model
• DOM Level 1
– Allows access to & manipulation of all HTML
elements
– Supported by virtually all browsers.
• DOM Level 2
– Adds style sheet object model
– Adds an event model
• DOM Level 3
– Mainly for XML - Adds content model (DTD &
Schemas) and validation
DOM Nodes
• EVERYTHING in an HTML document is a node,
including the document itself.
– Elements
– Attributes
– Text
– Comments
• All nodes can be referred to directly or in
relation to one another (parent, child, sibling,
etc.)
Accessing Nodes
• Standard methods (read, cross-browser
compatible) for accessing DOM nodes:
– getElementById(id) : returns a node
– getElementsByTagName(tag): returns an array of
nodes.
– parentNode, firstChild, lastChild, previousSibling,
nextSibling
– Root Nodes
• document.documentElement
• document.body
Accessing Nodes (continued)
• Methods and properties for accessing a
collection of nodes:
– hasChildNodes() : does the node have children?
– childNodes[x] : a nodeList of children
• window.document.childNodes[0].childNodes[1]
– attributes: a collection of a node’s attributes.
• getAttribute(attr);
• hasAttribute(attr);
• setAttribute(attr, value);
• removeAttribute(attr);
Accessing Node Information
• We can get the name, value, and type for any
node:
– nodeName : tag name, attr name, #text, #document
– nodeValue : (attribute & text nodes only) value of
attribute or inner text of text node.
– nodeType : returns a number
• Element = 1
• Attribute = 2
• Text = 3
• Comment = 8
Creating Nodes
• We can create nodes and add them to our DOM
document:
– createElement(tag) : create a new HTML element
– createTextNode(‘string’)
– appendChild(new_node)