How to implement search in offline HTML documents?
I need to write a tool that exports some 开发者_开发百科chats into offline HTML files. Is there a possibility to add search support (e.g. add some home page index.html
that contains a search field and shows search results)? The question is in common: what tools/frameworks/smth other can you advice for this?
I assume you want to do the search in a browser, because if not, then the grep
utility would be very efficient.
You can add a JavaScript to every exported page, which will scan the content and do the search. You can also handle multiple pages by creating an index page with "search data" and links to every subpage, but it depends on the amount of text you want to search. Searching on a single page will be easier.
The basic workflow will be:
- intercept "change" event of your search control (some
<input>
) - loop through your data elements (
<tr>
in case of tables,<li>
if you use lists, and so on) and check whether the typed text appears intextContent
property of this element.
This is the most basic form.
A little more efficient way would be to prepare an index array (by the script, in memory) where you store the textContent
(or anything you want to do your search on) and the element which contains it
When looping over the elements hide the elements which do not match, and unhide elements which do (for example add or remove the class "filtered").
I would use Javascript and build a searchindex. Have a look at this example (german only, but I think you will get the idea).
精彩评论