Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    findText function

    I am trying to allow somebody to search a webpage for certain words. I have succeeded in searching the document for a specific word in the text range, but am having trouble search for the word again if it appears later throughout the document. Here's the code below:

    <html>
    <head>
    <title>New Page 1</title>
    <script>
    function performSearch(){
    var textRange = document.body.createTextRange();
    if (textRange.findText("ben")){
    textRange.select();
    if (textRange.findText("ben")){
    textRange.select();
    }
    }
    }
    </script>
    </head>
    <body>
    <p><input type="button" value="Button" name="B3" onclick="performSearch();"></p>
    <p>dfgdfgdfgbendxfgfgdbenfsdfdben</p>
    </body>
    </html>

    The above code only highlights the first "ben" found in the document, but I would like it to highlight the second "ben" in the document.

    Any help with this matter would be most appreciated.

    Many Thanks in advanced,
    Ben Chivers

  • #2
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't suggest this!

    Code:
    <html>
    <head>
    <title>New Page 1</title>
    <STYLE TYPE="text/css" TITLE="">
    .foo {background-color: pink;}
    </STYLE>
    <script>
    function performSearch(what){
    r = new RegExp(what,"gi");
    document.body.innerHTML = document.body.innerHTML.replace(r,"<span class='foo'>"+what+"</span>");
    }
    </script>
    </head>
    <body>
    <p><form>
    <input type="button" value="Button" name="B3" onclick="performSearch('ben');">
    </form></p>
    <p>dfgdfgdfgbendxfgfgdbenfsdfdben</p>
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd prefer to let the regexp handle the entire replace

    r = new RegExp( "("+what+")" ,"gi");
    document.body.innerHTML = document.body.innerHTML.replace(r,"<span class='foo'>$1</span>");

    This way the case-sesitivity of the replacements is preserved.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Find text inside page script is this something you want to check out??
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •