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();
}
})(this);
});
}
/**
* 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:
// http://ejohn.org/blog/jquery-livesearch/
// 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 () {
generate_cache();
$('#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.
highlight_node(node);
});
});