Searching Body Text with textRange: Building on the Script and the VBScript Alternative - VBScript Continued
(Page 4 of 4 )
The main difference between subs and functions is that functions return values whereas subs do not. The set keyword is used to set the value of the range variable equal to the textRange we have defined.
Another difference is the way in which alerts are defined. The MsgBox function is used in place of the alert method and you’ll see that this gives you greater control over the general working of the alert. Several of the MsgBox elements have been set as vbExclamation and several as vbCritical. Not only does this change the icon that appears on the alert, it also changes the sound that is associated with it (provided the users’ sound settings cater for this.) Also, instead of specifying a return of false following a search term not being entered, you can simply exit the subroutine. Syntactically, these are the main differences between the two scripts. The search window does not need to be changed in anyway; the JavaScript ModalDialog window will happily pass the required value back to the VBScript file without causing any problems. You may also have noticed that some of the additional error handling if statements have been left out as the some of the user actions do not cause errors with the VBScript in the same way as they do with JavaScript.
Before this will work properly, you need to adjust the main HTML page that calls the script. Firstly, the file referenced in the script src attribute needs to be changed to behaviour.vbs or the JavaScript file will still be called, secondly, you need to remove the onclick event handlers from the button code. The subs in the VBScript file have the onclick events tacked on to the end of the button names in the first line of the sub.
As a point of interest; once a word has been found and highlighted, it is easy to use the range to replace the word searched for as well. Theoretically, another button could be added to the example web page that called a textReplace() function. The code this contained could produce a second dialog window which asked for a word to replace the highlighted word with.
It is then simply a case of setting range.text equal to the new word variable. While it is difficult to think of the point in using the replace method in this example, I’m sure there are cases when it would be useful, a simple script-based spell checking function added to form text area’s is one possibility.
As I mentioned in the first article, both of these scripts will work only on IE browsers. The JavaScript version may work on IE on other platforms such as the MAC (I don’t know as I haven’t tested it), but the VBScript will only work on MS platforms. The most popular alternative browser to IE at the moment is FireFox, a browser that uses the Gecko rendering engine to draw the various elements that make up web pages in the browser. This is also used by Netscape and Mozilla browsers so having a script that will be compatible with Gecko will clear up any compatibility issues between all major browsers. Provided you have some kind of detection model in place that can refer different browsers to different areas of your site, you can have the IE only script of choice running on one section, and the Gecko based script residing on another section. A gecko compatible script is the subject of discussion in article three.
| 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. |