Scott Hanselman

Added Search to my Blog, PLUS Highlighting

January 10, '04 Comments [5] Posted in ASP.NET | DasBlog | Javascript | XmlSerializer
Sponsored By

Chris Brooks mentioned today that I need to add Search to my blog.  Until I get my Google Juice up, he's totally right.  So, I added the new dasBlog 1.5 standard search (notice the search box to the left) and then took the Google Highlighting javascript code and modified it thusly:

function googleSearchHighlight() {
if (!document.createElement) return;
ref = document.referrer;
//START Added by Scott Hanselman
if
(ref.indexOf('?') == -1 || ref.indexOf('hanselman.com') != -1) //catch when I'm my OWN referrer!
{
ref = document.location.href;
if (ref.indexOf('?') == -1) return;
}
//END Added by Scott Hanselman
qs = ref.substr(ref.indexOf('?')+1);
qsa = qs.split('&');
for (i=0;iqsip = qsa[i].split('=');
if (qsip.length == 1) continue;
if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
for (w=0;whighlightWord(document.getElementsByTagName("body")[0],words[w]);
}
}
}

So this will highlight words on my site when you search from Google, Yahoo, or use the internal search.  
Give it a try!  Here's a link that searches for XmlSerializer.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Saturday, January 10, 2004 7:09:52 PM UTC
Where did you add that JavaScript code? To what page?
Sunday, January 11, 2004 4:41:28 AM UTC
The javascript gets added to EVERY page by putting it in the "Home" template.
Sunday, January 11, 2004 7:39:55 AM UTC
Cool thanks. I got this checked in now...
Tuesday, January 13, 2004 12:43:20 AM UTC
Is there a way to implement this using database search results?
I get my search results in the form of a dataset and bind that to a dataRepeater. Is it possible to include highlighting in my vb.net code?
Thursday, July 07, 2005 1:11:03 PM UTC
Is there a tool that you use to paste your code into your blog with the formatting.
Nishith
Comments are closed.

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