Spot the web RSS 2.0
# Wednesday, February 13, 2008

You see it in Google search results and a lot of other sites that have good search functionality. When you perform a search, your words or phrases are highlighted in the search results making it easy for you to find the most relevant content.

Today I’m going to show you a simple way to add this to your website or blog so your users can find what they need in style. I think that this kind of thing should be implemented more often for how easy it is to implement.

Here we go!

The JavaScript code:

  1. function highlightOnLoad() {  
  2.   // Get search string  
  3.   if (/s\=/.test(window.location.search)) {  
  4.     var searchString = getSearchString();  
  5.     // Starting node, parent to all nodes you want to search  
  6.     var textContainerNode = document.getElementById("content");  
  7.     // The regex is the secret, it prevents text within tag declarations to be affected

  8.     var regex = new RegExp(">([^<]*)?("+searchString+")([^>]*)?<","ig");  
  9.     highlightTextNodes(textContainerNode, regex);  
  10.   }  
  11. }  
  12. // Pull the search string out of the URL  
  13. function getSearchString() {  
  14.   // Return sanitized search string if it exists  
  15.   var rawSearchString = window.location.search.replace(/[a-zA-Z0-9\?\&\=\%\#]+s\=(\w+)(\&.*)?/,"$1");  
  16.   // Replace '+' with '|' for regex  
  17.   return rawSearchString.replace(/\+/g,"\|");  
  18. }  
  19. function highlightTextNodes(element, regex) {  
  20.   var tempinnerHTML = element.innerHTML;  
  21.   // Do regex replace  
  22.   element.innerHTML = tempinnerHTML.replace(regex,">$1<span class='highlighted'>$2</span>$3<");  
  23. }  
  24. // Call this onload, I recommend using the function defined 
    // at: http://untruths.org/technology/javascript-windowonload/  

  25. addOnLoad(highlightOnLoad()); 

Now, the CSS:

  1. span.highlighted {  
  2.   background-color#161616;  
  3.   font-weightbold;  

Code explanation

First, the highlightOnLoad function checks window.location.search to see if we need to be running any of this stuff, then calls getSearchString to get a sanitized search string so that nothing funky can happen if, say, the user searches for ‘<script>’. You should really be sanitizing all search inputs at least on the back-end anyway.

Then, the highlightTextNodes function uses a regex replace on our textContainerNode’s innerHTML. The regex verifies that the text is between a > and a < (and not the other way around). Actually nice and simple!

Caveats

This may end up being a bit slow if you are doing this on a LOT of text, but for my blog text, it seems quite snappy to me. Also, the CSS does not bold text inside links, but the background color is there to make it obvious.

Wednesday, February 13, 2008 5:06:52 PM (Jerusalem Standard Time, UTC+02:00)  #    Comments [0] - Trackback
CSS | Javascript | Programming
Comments are closed.
Navigation
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Guy Levin
Sign In
Statistics
Total Posts: 63
This Year: 0
This Month: 0
This Week: 0
Comments: 14
Themes
All Content © 2010, Guy Levin