“Search on this Page” using jQuery

, , ,

Every modern browser provides the ability to search for text within a page. In most cases that functionality is underserved and allows you to find only one instance of the text at a time. Google Chrome recently presented improved version of search which searches for the given text as you type and highlights all instances found on the current page. But wait a minute: is this functionality limited to Google Chrome only?

Check out my article @ End User SharePoint.com: “Search on this Page” using jQuery

Technorati Tags: , ,

Possibly related posts

9 Responses to ““Search on this Page” using jQuery”

  1. James Says:

    It's an interesting feature, one that I'd love to use but, alas, I am (for once) crippled by mac and can't use chrome at the moment

  2. Waldek Mastykarz Says:

    @James: I have created a JavaScript equivalent of the Google Chrome search highlight. You can use the JavaScript in any browser.

  3. Christoph Says:

    Waldek,

    I really love your site – it's an abounding source of know how and tips. I tested the demo of this script on endusersharepoint.com and it would only work when I entered one search term in the box. Any ideas why it would not work once a space is entered? 'Lorem ipsum' should definitely be found…

  4. Waldek Mastykarz Says:

    @Christoph: Thank you for the feedback, Christoph. I appreciate it. As for your question: I believe that the problem has to do with the regular expression which runs the search. I think that the regex engine doesn't recognize spaces as valid characters and would rather expect \s (which could be of course replaced on-the-fly but somehow slipped my mind ;-) ).

  5. Suraj Kurapati Says:

    Hello Waldek,

    I want to use this library in my open source project, but it isn\'t clear what license your code was released under (I\'m assuming public domain since you included it in a tutorial).

    If there isn\'t a particular license attached to the code, will the following file header be sufficient attribution? Thanks.

    <pre>
    /*
    \"Search on this Page\" plugin for jQuery by
    Waldek Mastykarz <http://blog.mastykarz.nl>

    Published online, on 17 November 2008, at
    <http://www.endusersharepoint.com/?p=951> and
    <http://blog.mastykarz.nl/search-page-jquery/>

    This source code was extracted directly from this demo page:
    <http://blog.mastykarz.nl/samples/jquery/searchOnThisPage.html>
    */
    </pre>

  6. Waldek Mastykarz Says:

    @Suraj: the header you've mentioned is okay with me. Please note, that by including the code in your custom solution you are the one responsible for it, meaning providing any fixes, support, etc.
    Glad I could help!

  7. tfforums Says:

    Hi

    There are 2 main issues with this script.

    1 – it searches within the HTML tags them selves, partially fixed with:

    var htmls= $(this).html();
    var text= $(this).text();
    if(text.match(regx))
    {
    //original replacement code
    }

    That way it has to match the text and then replaces on the html – muuuch better but still not 100%

    2) if the text wrapper matches multiple – the object only stores the original html for the first instance… fixed by:
    // — public methods
    this.resetOriginalText= function() {
    var instance = this;

    $(instance.textWrapper).each(function(i){$(this).html(instance.originalText[i]);});
    }

    and

    this.setOriginalText= function() {
    var instance=this;
    $(".ms-vb,.ms-vb2").each(function(i){

    instance.originalText[i]=$(this).html();
    });
    instance.set=true;
    }

  8. tfforums Says:

    OH – i forgot
    var searchText = escape(this.value);
    searchText=searchText.replace(/%20/gi,"\\s");

    makes it search over spaces too.

  9. Waldek Mastykarz Says:

    @tfforums: great suggestions. Thanks for the feedback.

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2010 Waldek Mastykarz

Creative Commons License