Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently designing a search algorithm for a document, and just got curious about this while designing the algorithm: how do web browsers search a webpage?

For example, in Google Chrome, you can press the keyboard shortcut Ctrl-F to activate the "Find" bar, which will let you search a particular webpage for text. How does it do that, given that it only has the raw HTML as reference?

share|improve this question

migrated from Feb 15 '13 at 9:56

This question came from our site for computer enthusiasts and power users.

When you say that you are designing a search algorithm for a document, could be more specific? Are you doing in within the webpage itself or are you doing this as a server-side operation? Also, are you just trying to find the page/document that matches best or are you searching for a particular element within the page to highlight, much like Firefox and Chrome do?

As @iglvzx mentioned, you should look into DOM. I've setup a very simple example below for implementing a search on a page. The main point I'm trying to show is building up a cache object that is a hash with the DOM element/node as the key and it's searchable text as the value. As for the actual search algorithm, I assume you are building your own but I do have a link to a project for live-searching in JavaScript that could be of good help.

I'm sure there are plenty of problems with the below code when it comes to actual application, but hopefully this can help point you in the right direction.

// setup a global search-cache
var cache = null;

 * Public: Generate the cache from the existing page. Note, this may need
 *         to be re-run if the DOM (page) changes.
 * Returns nothing; populates a global cache object
function generate_cache() {
    var cache = {};

    $('body').children().each(function () {
        (function process_node(node) {
            var has_children = node.children().length > 0;
            if (has_children) {
                node.children().each(function () { process_node(this); });
            else {
                cache[node] = node.text();

 * Public: Search the cache of the page
 * value - The value to search for within the cache
 * Returns the node the text was found in or with the highest score
 * or whatever your search algorithm does.
function search (value) {
    // You're search-code here...
    // I'd recommend that you checkout quicksilver for
    // scoring your results and getting the highst-matching
    // node. You can find the code here:
    // which you'll need to adapt to your situation

 * When the document loads, build your cache and bind a keyup event
 * to you search-box.
$(document).ready(function () {
    $('#search-box').keyup(function() {
        var node = search($('search-box').val());

        // I assume you'll wan to do something with the node
        // once you've found it so I just made up a function
        // for highlighting the entire node.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.