topical media & game development

talk show tell print

professional-javascript-10-DOMRangeExample3.htm / htm



  <html>
      <head>
          <title>DOM Range Example</title>
          <script type="text/javascript">
              function useRanges() {
                  var oP1 = document.getElementById("p1");
                  var oHello = oP1.firstChild.firstChild;
                  var oWorld = oP1.lastChild;
                  var oRange = document.createRange();
                  
                  oRange.setStart(oHello, 2);
                  oRange.setEnd(oWorld, 3);
                  
                  document.getElementById("txtStartContainer1").value = oRange.startContainer.nodeValue;
                  document.getElementById("txtStartOffset1").value = oRange.startOffset;
                  document.getElementById("txtEndContainer1").value = oRange.endContainer.nodeValue;
                  document.getElementById("txtEndOffset1").value = oRange.endOffset;
                  document.getElementById("txtCommonAncestor1").value = oRange.commonAncestorContainer.tagName;
  
              }
          </script>
      </head>
      <body><p id="p1"><b>Hello</b> World</p>
          <input type="button" value="Use Ranges" onclick="useRanges()" />        
          <table border="0">
          <tr>
              <td>
                  <fieldset>
                      <legend>oRange1</legend>
                      Start Container: <input type="text" id="txtStartContainer1" /><br />
                      Start Offset: <input type="text" id="txtStartOffset1" /><br />
                      End Container: <input type="text" id="txtEndContainer1" /><br />
                      End Offset: <input type="text" id="txtEndOffset1" /><br />
                      Common Ancestor: <input type="text" id="txtCommonAncestor1" /><br />    
                  </fieldset>
              </td>
  
          </tr>
          </table>
          <p><strong>Note:</strong> This example uses DOM ranges and will only work in browsers that support DOM ranges. This example will fail in Internet Explorer.</p>
  
      </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.