Searching Body Text with textRange, part 1: The Basic Script - Main Script Variants
(Page 2 of 5 )
I will provide the main script in both JavaScript and VBScript variants. Which one you choose to use will depend largely on personal preference as both scripts work equally as well.
I should point out now that this script (both the JavaScript and VBScript variants) will only work on Microsoft IE browsers. Not only is the textRange object only implemented by IE, and the findText method also only used in IE (although other browsers do support a createRange method), but the showModalDialog function that is used to call the search window is also an IE only method (although, the prompt method could be used in place of this to produce a dialog box to capture the search word.)
In reality, this issue should only affect a minority of your website visitors, as current trend analysis reveals that up to 88 percent of US-based surfers still use IE as their primary browser. This figure is dropping slowly as more users take up Firefox, and part of the current 88 percent may actually be using the Opera browser, which often reports itself as IE. Nevertheless, while designing scripts that will only run on certain users' computers is not an ideal scenario to find yourself in, I’m sure you’ll agree that it is better to include something that will improve the experience of most visitors, then to not include it at all.
Additionally, your site may have some kind of browser-detection model in place that can re-direct visitors to specific parts of your site that cater to their browser type. The code contained and explained in this article could happily reside on an IE-only page, while an equivalent script that works on Gecko-based browsers is in place on other pages. A Gecko-friendly script will be the subject of article three in this series.
Next: Some Material to Search >>
More Code Examples Articles
More By Dan Wellman