Searching Body Text with textRange: Enter the Gecko - The second function
(Page 3 of 4 )
The second function can be added with the following code:
function searchNext() {
if (button1clicked != 1) {
window.alert("No search word entered!");
} else {
If (searchWord == null) {
Window.alert("No search word entered!");
return false
}
match = window.find(searchWord, false, false, false, true, false, false);
if (match == false) {
window.alert("No more matches");
}
}
}
It first checks that the flag is set to one and produces an appropriate message if not. Providing the flag is set, it simply calls the find method of the window object again. Not only is this method smart enough to do the searching and highlighting on its own, it is able to keep track of words it has found already and automatically move on to and highlight the next occurrence of the specified search word! Again, this cuts down considerably on our code. The script will continue highlighting matches until it finds no more and produces the final alert to say that no more instances of the word exist.
That’s it. Save the file as behaviour(moz).js and amend the HTML file to link to the new one. Open the HTML page in any of the supported Gecko based browsers and see it in action.
Next: Create a master script file >>
More Code Examples Articles
More By Dan Wellman