web development -2
HTML5, CSS3, and JavaScript
6th Edition
Working with Document Nodes and Style Sheets
Tutorial 12
Carey
XP
XP
XP
XP
XP
Objectives
Explore nodes and the node tree
Create element and text nodes
Append nodes to a web document
Work with the properties and methods of element nodes
Create attribute nodes
2
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
2
Objectives (continued)
Add attribute nodes to page elements
Create external and embedded style sheets with JavaScript
Add style sheets to a web document
Create a style rule for an embedded style sheet
Enable and disable style sheets
3
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
3
Nodes and Document Structure
Node: Any object within an HTML file
Node tree: Hierarchical structure of nodes
4
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
4
Nodes and Document Structure (continued 1)
Nodes in the node tree have a familial relationship
Each node can be a parent, child, and/or sibling of other nodes
Node relationship is indicated using the following expression:
node.relationship
where node is a currently selected node and relationship is the relationship of another node to the current node
5
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Nodes and Document Structure (continued 2)
Root node is the base node from which all other nodes originate
Root node is an elementary node in the hierarchy of a node tree
Each node can contain one or more child nodes to transfer the required information
6
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
6
Nodes and Document Structure (continued 3)
7
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
7
Nodes and Document Structure (continued 4)
8
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
8
Restructuring the Node Tree
Working with nodes is better than writing HTML code through the innerHTML property
Node tree can be rearranged to form a new structure
Restructuring a node tree helps in ordering an HTML content with more accuracy
9
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Restructuring the Node Tree (continued 1)
10
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Restructuring the Node Tree (continued 2)
makeOutline() function is used to generate the code for the document outline
11
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating and Appending Nodes
JavaScript’s document object model supports several methods to create nodes of different types
12
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating and Appending Nodes (continued 1)
13
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating and Appending Nodes (continued 2)
Nodes that are created are added to the computer memory as a document fragment
Document fragment is not a part of the document’s node tree until it is appended to a node within the tree
14
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating and Appending Nodes (continued 3)
15
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Node Types, Names, and Values
Text for each entry in the outline list should match the text of a heading in the source article
To maintain uniformity throughout a document, outlining app does the following:
Loops through the child nodes of the source article
16
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
16
Working with Node Types, Names, and Values (continued)
Tests whether each child node represents an h1 through h6 element node
Extracts the element’s text if it is a heading and creates a list item containing that same text string
Appends the list item as a new child of the outline’s ordered list
17
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
17
Looping through the Child Nodes Collection
Use a counter variable starting with a value of 0 and increase by 1 for each node, up to the length of the childNodes collection
for (var i = 0;
i <node.childNodes.length; i++)
{
Commands
}
18
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
18
Looping through the Child Nodes Collection (continued 1)
Object reference for child nodes in the for loop is
node.childNodes[i]
where node is the parent node of the child nodes collection, and i is the value of the counter variable in the for loop
19
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
19
Looping through the Child Nodes Collection (continued 2)
Use familial references, starting with the first child of the parent node and traverse each subsequent sibling until no siblings remain
for (var n = node.firstChild; n !== null; n = n.nextSibling) {
commands
}
Object reference for child nodes in the for loop is n variable defined within the for loop expression
20
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
20
Looping through the Child Nodes Collection (continued 3)
21
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Node Properties
The node has the following properties:
node.nodeType: It indicates the type of node
node.nodeName: It indicates the node name
node.nodeValue: It indicates the node value for a node in the document
To check whether a node refers to an element, attribute, text string, comment, document, or any other type of node, use
node.nodeType
22
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
22
Node Properties (continued 1)
To retrieve the name of a node within a document, use
node.nodeName
To retrieve a node’s value, use
node.nodeValue
23
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
23
Node Properties (continued 2)
24
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
24
Node Properties (continued 3)
25
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
25
Node Properties (continued 4)
Text node can be referenced within an element in order to access the text contained within the element
Example:
<h1 id=”title”>History Online</h1>
Text of the above h1 heading is obtained by
document.getElementById(“title”). firstChild.nodeValue;
26
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Node Properties (continued 5)
27
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
27
Node Properties (continued 6)
28
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating a Nested List
29
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
29
Creating a Nested List (continued 1)
In a nested list, lower-level headings are nested within upper-level headings
All h1 heading are placed at the top most level of the table of contents, all h2 headings are placed at the next lower level, and so forth
indexOf()function is used to determine the level of each list item
30
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
30
Creating a Nested List (continued 2)
To test the three possibilities for the comparison of the index numbers, use:
if (headLevel === prevLevel) {
// Append the list item to the current list
} else if (headLevel > prevLevel) {
// Start a new nested list
} else {
// Append the entry to a higher list
}
31
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
31
Creating a Nested List (continued 3)
32
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
32
Creating a Nested List (continued 4)
In JavaScript, following commands are used to append a nested list:
var nestedList = document.createElement(“ol”);
NestedList.appendChild(listElem);
outlineList.lastChild.appendChild(ne stedList);
outlineList = nestedList;
33
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
33
Creating a Nested List (continued 5)
Use the following command to calculate the difference between the index number of the previous heading and the current heading:
var levelUp = prevLevel - headLevel;
Use the parentNode property twice for each difference in level to move up the node tree, as shown below:
for (var i = 1; i <= levelUp; i++) {
outlineList = outlineList.parentNode.parentNode;}
34
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
34
Creating a Nested List (continued 6)
for (var i = 1; i <= levelUp; i++)
{
outlineList = outlineList.parentNode.parentNode;
}
Use the following command to append a list item to a higher-order list:
outlineList.appendChild(listElem);
35
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
35
Working with Attribute Nodes
Attribute node contains an attribute that can be attached to an element node
Attribute nodes are not part of the node tree because they are not the child or parent of any node
Attribute nodes cannot be placed in a node hierarchy
36
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
36
Working with Attribute Nodes (continued 1)
37
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Attribute Nodes (continued 2)
Document object model supports several methods to create, attach, and set the values of attributes
38
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating Heading IDs
Creating hypertext links involves two steps,
Adding an ID attribute to each heading (if it doesn’t already have one) to mark its location within the web page.
Changing the content of each list item to a hypertext link pointing to the corresponding heading
39
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating Heading IDs (continued 1)
40
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
40
Creating Heading IDs (continued 2)
Counter variable headNum is created to maintain the uniqueness of each ID
headNum is set with initial value = 0, and it increases by 1 for each heading found in the source document
Use the following code to create the headNum variable:
var headNum = 0;
41
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating Heading IDs (continued 3)
42
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Looping through the Attributes Collection
To examine each HTML attribute associated with an element, use the following code:
node.attributes
Named node map is an unordered list of nodes that can be referenced by their names
Named node map refers to the names of attributes in an attributes collection
Index numbers are also used to refer to the attributes
43
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
43
Creating Hypertext Links
Each list item can be changed to a hypertext link pointing to the corresponding heading using the href attribute
Value of the href attribute for each list item is #id where id is the ID value assigned to the corresponding heading
44
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
44
Creating Hypertext Links (continued 1)
45
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Creating Hypertext Links (continued 2)
46
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Style Sheets
Inline styles are created by modifying the style property of various page elements
In inline styles, the style property is applied to specific elements in a document
JavaScript can be used to create and modify style sheets that can be applied to en entire document
47
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
47
The styleSheets Collection
Both external and embedded style sheets are part of the following object collection:
document.styleSheets
Arrangement of style sheets within a collection follows their order of declaration within a document head
First style sheet declared in a document is referenced as document.styleSheets[0];
48
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
48
The styleSheets Collection (continued 1)
Subsequent style sheets would be referenced as document.styleSheets[1], document.styleSheets[2],and so forth
Total number of style sheets in a document is given by the document.styleSheets.length property
Last style sheet in a collection can be referred using the following property:
document.styleSheets[document.styleS heets.length-1]
49
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
49
The styleSheet Object
Each style sheet in a styleSheets collection is a styleSheet object
50
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The styleSheet Object (continued 1)
To reference a specific style sheet, use the following code:
document.styleSheets[index]
where index is the index number of the style sheet, starting with 0 for the first style sheet
Style sheet switcher is an application used to choose among different style sheets to display a web document
51
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
51
The styleSheet Object (continued 2)
52
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The styleSheet Object (continued 3)
53
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The styleSheet Object (continued 4)
Form buttons are used to switch between the two views of a page
HTML code for the form buttons is as follows:
<div id=”styleButtons”>
<input type=”button” value=”Web View” />
<input type=”button” value=”Page View” />
</div>
54
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
The styleSheet Object (continued 5)
55
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Working with Style Sheet Rules
Style rules within the sheet are part of a following object collection:
stylesheet.cssRules
where stylesheet is a reference to a sheet within the document.styleSheets object collection
56
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
56
Working with Style Sheet Rules (continued 1)
Example: If the first style sheet contains the following rules:
<style id=”hStyles”>
h1 {color: red;}
h2 {color: blue;}
</style>
then the following object reference document.styleSheets[0].cssRules[1]
points to the second style rule
h2 {color: blue;}
57
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Style Rule Objects
Each individual rule within a cssRules collection is treated as an object
58
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Adding and Removing Style Rules
To add a new rule at the end of a style sheet, use sheet.cssRules.length as the index value
Once a rule has been removed, all subsequent rules move up in the cssRules collection and their index values change subsequently
59
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
59
Adding and Removing Style Rules (continued 1)
Use the following code to insert a new style rule into a style sheet:
sheet.insertRule(rule, index)
where sheet is the style sheet, rule is the text of the style rule, and index is the index value of the new rule
Use the following code to remove a rule from a style sheet:
sheet.deleteRule(index)
where index is the index value of the rule
60
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
60
Adding and Removing Style Rules (continued 2)
61
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
JavaScript can be used to append an embedded style sheet to the end of a document head
XP
XP
XP
XP
XP
61
Adding and Removing Style Rules (continued 3)
62
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Adding and Removing Style Rules (continued 4)
63
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
Exploring Calculated Styles
Final appearance of any page element is a calculated style based on all the styles found within the following:
External style sheets
Embedded sheets
Inline styles
Styles built into the browser
64
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
64
Exploring Calculated Styles (continued 1)
Following getComputedStyle() method can be used to retrieve the calculated styles:
document.getComputedStyle(object, pseudo)
where object is a page object, and pseudo is a text string containing a pseudo-element that is applied to the page object
If no pseudo-element is used, set the pseudo value to null
65
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
65
Exploring Calculated Styles (continued 2)
To retrieve the calculated styles for the first paragraph of the current document, use:
var p1 = document.getElementsByTagName(“p”)[0 ];
var p1Styles = document.getComputedStyle(p1, null);
66
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
66
Exploring Calculated Styles (continued 3)
To retrieve the calculated value of a specific style, use:
styleDeclaration.getPropertyValue (styleText)
or
styleDeclaration.style
where styleDeclaration is a CSSStyleDeclaration object, styleText is a text string, and style is the JavaScript name of a style property
67
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
67
Exploring Calculated Styles (continued 4)
Calculated styles are read-only values and cannot be changed using JavaScript
Values are expressed in absolute units
Calculated property that measures a size is expressed only in pixels
68
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
XP
XP
XP
XP
XP
68