Code Examples
  Home arrow Code Examples arrow Page 2 - Searching Body Text with textRange: Buildi...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Searching Body Text with textRange: Building on the Script and the VBScript Alternative
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2005-06-30

    Table of Contents:
  • Searching Body Text with textRange: Building on the Script and the VBScript Alternative
  • Add a Flag
  • The VBScript
  • VBScript Continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Searching Body Text with textRange: Building on the Script and the VBScript Alternative - Add a Flag


    (Page 2 of 4 )

    Fortunately, this is fairly easy.  All you need to do is add a flag to the first function that is set when that function is called (when the find button is clicked.)  If the second button is clicked and that flag has not been set, you can produce an alert that tells the user to click the find button first and enter a search term, thus preventing the error.  First add another global variable to the top of the page:

    var button1clicked

    Now, in the first function (textSearch), set the flag to 1 at the beginning of the function:

    button1clicked = 1

    This tells the script that the find button has definitely been clicked.  Now a simple if statement at the top of the second function to test the value of the flag variable quickly lets the script accordingly if the first button has not been clicked:

    if (button1clicked != 1) {
        window.alert ("Please enter a search value first");
        return false
      }

    Like we added an if statement in the first article to ensure that the page did not error when no value was passed back to a variable that was expecting one, this statement simply checks that button1clicked does equal 1, but if not, alerts the user.

    Finally, if a user clicks the x button at the top of the dialog box instead of clicking the cancel button an alert informs the user that ‘undefined’ couldn’t be found, which is rather silly as undefined was not entered as a search term.  This happens because in this case, as when the user searches for a word that is not found in the body text, match does equal false and the appropriate alert is fired.  It would be better all round if nothing happened when the x button was clicked, just like when the cancel button is clicked.  Change the first if statement in the first function to this:

    if (searchWord=="") {
      return false
      } else if (searchWord==null) {
      return false
    }

    Now test it.  If you search for a word that is not present in the text, you still get the "couldn’t find ‘word’" message, and if you click the x button or the cancel button on the search dialog, nothing happens and no errors occur.  Similarly, the second if statement of the second function can also be modified in the same way so that if a user opens the search dialog window (thus setting button1clicked to 1) but then closes it without entering a search word and then clicks the find next button, an error message more appropriate is displayed:

    if (searchWord=="") {
      window.alert ("No Search value entered");
      return false
    } else if (searchWord==null) {
      window.alert("No search value entered");
      return false
    }

    The script should now be able to handle anything a user throws at it.

    More Code Examples Articles
    More By Dan Wellman


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT