Here are some examples for DOM 2 Range in JavaScript. I will add to them as I get a chance. Send bugs and comments to dylans@yahoo.com
Example 1, Try everything: (note, this now works on recent mozilla nightly builds (mid-late May 2001)
<script type="text/javascript> alert('hasFeature: '+document.implementation.hasFeature("Range","2.0")); var x = document.createRange(); x.setStart(document.getElementsByTagName("p")[0],0); x.setEnd(document.getElementsByTagName("p")[0],1); alert('startContainer: '+x.startContainer); alert('innerHTML: '+x.startContainer.innerHTML); alert('startOffset: '+x.startOffset); x.setStartBefore(document.getElementsByTagName("p")[0]); x.setEndBefore(document.getElementsByTagName("p")[0]); x.collapse("TRUE"); x.collapse("FALSE"); alert('startOffset: '+x.startOffset); alert('collapsed: '+x.collapsed); alert('selectedNode: '+x.selectNode(document.getElementsByTagName("p")[0])); x.selectNode(document.getElementsByTagName("p")[0]); x.selectNodeContents(document.getElementsByTagName("p")[0]); alert('selectNodeContents: '+x); var y = document.createRange(); y.setStart(document.getElementsByTagName("script")[0],0); y.setEnd(document.getElementsByTagName("script")[0],1); alert('compareBoundaryPoints(0,x): '+y.compareBoundaryPoints(0,x)); alert('compareBoundaryPoints(1,x): '+y.compareBoundaryPoints(1,x)); alert('compareBoundaryPoints(2,x): '+y.compareBoundaryPoints(2,x)); alert('compareBoundaryPoints(3,x): '+y.compareBoundaryPoints(3,x)); x.deleteContents(); x.extractContents() x.cloneContents(); alert(document.body.innerHTML); alert(x); x.insertNode(document.getElementsByTagName("div")[0]); x.surroundContents(document.getElementsByTagName("div")[1]); var y = x.cloneRange() alert(y.toString()); y.detach(); document.getElementsByTagName("div")[0].normalize(); alert(y);
These examples are from bugzilla courtesy of Anthony D. at Netscape.
Example 2: selectNode example
Example 3: selectNode example
Example 4: same as example 1 except it includes Jeff Yates' Range patch