Searching Body Text with textRange, part 1: The Basic Script - How it Works
(Page 5 of 5 )
The functionality of the page is simple. It has a script containing two functions; one function is mapped to the ok button and one to the cancel button. The ok function gives an alert if the input field is left blank, but providing a value has been entered, it passes this back to the element that called it in the first place, which as we know is the searchWord variable in the behavior file. The cancel function simply returns nothing and closes the dialog window.
Save this as searchwindow.htm in the same location as the other two files. Opening the findingtext.htm file, the text search can now be carried out. I think you'll agree, however, that there are several problems with the dialog box used to enter search terms. Primarily, the problem is that it doesn't look very good -- there are things on it that look bad and are not needed, like the status bar and the help button. You can tidy this up a little by including the following attributes in the showModalDialog method in the behaviour file:
status:no;help:no
Simply append this to the end of the bracketed code. This will improve the look of the search window slightly and remove elements that have no function or use. Using the search button on the initial Web page will now call a window that looks and behaves like a dialog and can be used to find a specific word in the block of text that makes up the body of the document.
There is a problem with this however; when the dialog window is open, if the ok button is clicked and there is nothing in the search input, an alert informs the user of this. But when the cancel button is clicked, the dialog simply closes and nothing is returned to the searchWord variable. When this happens, an error occurs, as there is nothing in the script to specify what should happen if this occurs. This is an important part of the errata issues we will look at in the next article; you have to build into your script every attempt to catch any errors that may occur as a result of improper use of your Web page. Add the following if statement directly below the searchWord declaration in the main script file:
if (searchWord=="") {
return false
}
Now the script knows what to do when an empty string is returned.
There are also limitations to the functionality of this script; primarily, there is nothing to take into account that there may be more than one occurrence of whatever word the visitor is searching for. Part two of this article goes on to explain how this functionality can easily be added, and goes on to discuss errata in more detail. The promised VBScript version also forms part of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |