Searching Body Text with textRange: Enter the Gecko (Page 1 of 4 )
This article focuses on turning the IE only body-text searching JavaScript file from the previous article into something that is supported and works on browsers using the Gecko layout engine. The conversion is surprisingly simple, and due to the advanced window.find() method compatible with Gecko, the script is actually smaller, with fewer moving parts!
Calling this article "Using the textRange Object to Search Body Text" is actually a bit of a misnomer, because there is no textRange object in the gecko DOM. However, as the script provides the same function as the scripts in the previous articles, it follows on to a sensible conclusion to what has been discussed already.
There is no textRange simply because one is not needed in browsers such as Mozilla, FireFox or Netscape. The find() method of the window object is advanced enough to be able to search through the text in the body of a document without even needing to capture the text using the textRange object. Ranges of any kind just don’t need to be used.
The script used here was written by myself but inspired by a script created by Jason Karl Davis (http://www.jasonkarldavis.com/) that uses DOM treewalking methods to traverse the text nodes of the document and match search words against their contents. DOM scripting has not been used in this example at all, simply because the power provided by it is not required in a search script of the same scope as the one discussed in parts one and two.
The script I will discuss has been tested on Mozilla 1.7.6 and 1.7.8, Firefox 1.0 and 1.0.4 and Netscape versions 7.2 and 8, and has been found to work as intended on all of them. To create a test version of this script, create a new js file. The variables can still be defined at the top of the file, but the range variable will not be needed:
var searchWord
var match
var button1clicked
Next: The first function >>
More Code Examples Articles
More By Dan Wellman