Find In Page Script

This cross browser DHTML script simulates the Edit> Find In Page feature of the browser to allow your visitors to more easily search for a particular text on your page.
As in the "Find In Page" feature, it highlights the searched text if found, otherwise, prompts a "Not Found" message. If your page contains lots of textual content, definitely consider adding this script to make it more search friendly.

Type in the text you wish to locate on this page:

<script type="text/javascript">
<!-- var NS = (document.getElementById &&! document.all) var IE = (document.getElementById || document.all) var n = 0 function findInPage(str){ var txt, i, found if (str == ""){ return false } // Find next occurance of the given string on the page, wrap around to the start of the page if necessary. if (NS) { // Look for match starting at the current point. If not found, rewind back to the first match. if (!window.find(str)){ while(window.find(str, false, true)) n++ } else{ n++ } if (n == 0){ // If not found in either direction, give message. alert("Not found.") } } if (IE) { txt = document.body.createTextRange() for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) { // Find the nth match from the top of the page. txt.moveStart("character", 1) txt.moveEnd("textedit") } if (found){ // If found, mark it and scroll it into view. txt.moveStart("character", -1) txt.findText(str) txt.select() txt.scrollIntoView() n++ } else{ // Otherwise, start over at the top of the page and find first match. if (n > 0) { n = 0 findInPage(str) } else{ // Not found anywhere, give message. alert("Selected Word Not found.") } } } return false } // --> </script> Place the following where you want the text bar to go. <form name="search"> <input name="txt" type="text" size=15 onChange="n = 0;"></font> <input type="button" value="Search" onclick="findInPage(document.search.txt.value)"> </form>