04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: DOM Levels 2 and 3<br />

The first and second range share the same starting point, so comparing them using<br />

compareEndPoints() returns 1. range1 ’ s end point occurs after range2 ’ s end point,<br />

so compareEndPoints() returns 1 .<br />

IE also has two additional methods for comparing ranges: isEqual() , which determines if two ranges<br />

are identically equal, and inRange() , which determines if a range occurs inside of another range. Here<br />

is an example:<br />

var range1 = document.body.createTextRange();<br />

var range2 = document.body.createTextRange();<br />

range1.findText(“Hello World”);<br />

range2.findText(“Hello”);<br />

alert(“range1.isEqual(range2): “ + range1.isEqual(range2)); //false<br />

alert(“range1.inRange(range2): “ + range1.inRange(range2)); //true<br />

This example uses the same ranges as in the previous example to illustrate these methods. The ranges are<br />

not equal because the end points are different, so calling isEqual() returns false. However, range2 is<br />

actually inside of range1 , because its end point occurs before range1 ’ s end point but after range1 ’s start<br />

point. For this reason, range2 is considered to be inside of range1 , so inRange() returns true.<br />

Cloning an IE Range<br />

Text ranges can be cloned in IE using the duplicate() method, which creates an exact clone of the<br />

range, as shown in the following example:<br />

var newRange = range.duplicate();<br />

All properties from the original range are carried over into the newly created one.<br />

Summary<br />

The DOM Level 2 specifications define several modules that augment the functionality of DOM Level 1.<br />

DOM Level 2 Core introduces several new methods related to XML namespaces on various DOM types.<br />

These changes are relevant only when used in XML or XHTML documents; they have no use in HTML<br />

documents. Methods not related to XML namespaces include the ability to create new instances of<br />

Document programmatically as well as enabling the creation of DocumentType objects.<br />

The DOM Level 2 Style module specifies how to interact with stylistic information about elements as<br />

follows:<br />

❑<br />

❑<br />

❑<br />

❑<br />

Every element has a style object associated with it that can be used to determine and change<br />

inline styles.<br />

To determine the computed style of an element, including all CSS rules that apply to it, there is a<br />

method called getComputedStyle() .<br />

IE doesn ’ t support this method but offers a currentStyle property on all elements that returns<br />

the same information.<br />

It ’ s also possible to access style sheets via the document.styleSheets collection.<br />

362

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!