Adding Search to my Blog

Posted Tuesday, January 11, 2005 10:08 PM by CumpsD
Important Notice:
If you want to add this to your blog, read:
http://www.google.com/services/terms_free.html
http://www.google.com/stickers.html
http://www.google.com/permissions/guidelines.html

As noted in the comments to this post, the code below violates their agreement. You can fix this by putting a Google logo of the stickers site next to your search, you can't modify the logo! If you use a search, you can only put Google on your site, not together with MSN.

If you do all of this, you're fine. Now you have three choices:
1. You don't care and use it like this in minmalistic form, some say your site will be removed from Google when doing so.
2. You make the changes I just said, and everything is fine, it just doesn't look that much finished anymore :)
3. You remove it all together. (my choice) 
The choice is yours, never complain to me if Google removes your site :p

Now back to the post.

After reading Heather's post about searching and taking a look at Geoff Appleby's JavaScript function I decided to add a Search box to my blog as well.

Here's a step by step explanation on how to add it:

  • Go to your blog's Admin section.
  • Go to Options.
  • Go to Configure.
  • Add the following to 'Static News/Announcement':
    1<h3>Search</h3> 
    2<input class="BlogSearch" type="text" name="searchBox" id="blogSearchText" value="" onkeypress="return blogSearch(event, this);">
    3<input type="button" value="Search" onclick="return blogSearch2('blogSearchText');" class="BlogSearchButton">
    4<script type="text/javascript">
    5
    6function blogSearch(event, oInput) {
    7 var keyCode = (event) ? event.keyCode : keyStroke.which;
    8 if (keyCode == 13) {
    9    top.location = 'http://www.google.be/search?q=' + escape(oInput.value) + '+inurl%3Acumpsd+site%3Aweblogs.asp.net';
    10    return false;
    11 } return true;
    12}
    13
    14function blogSearch2(oInputId) {
    15 var oInput = document.getElementById(oInputId);
    16 top.location = 'http://www.google.be/search?q=' + escape(oInput.value) + '+inurl%3Acumpsd+site%3Aweblogs.asp.net';
    17 return false;
    18}
    19</script>
  • Replace cumpsd with your blog url.
  • Save.
  • Admire your new search.
If you want to use MSN as a search engine use the following code (and replace cumpsd with your own url):
1<h3>Search</h3> 
2<input class="BlogSearch" type="text" name="searchBox" id="blogSearchTextMSN" value="" onkeypress="return blogSearchMSN(event, this);">
3<input type="button" value="Search" onclick="return blogSearchMSN2('blogSearchTextMSN');" class="BlogSearchButton">
4<script type="text/javascript">
5
6function blogSearchMSN(event, oInput) {
7 var keyCode = (event) ? event.keyCode : keyStroke.which;
8 if (keyCode == 13) {
9    top.location = 'http://search.msn.com/advresults.aspx?q=' + escape(oInput.value) + '%20cumpsd&FORM=SMCA&adv_f=all&adv_dom=weblogs.asp.net&adv_depth=&adv_dt=html&adv_dt=pdf&adv_dt=ppt&adv_dt=msword&adv_dt=x';
10    return false;
11 } return true;
12}
13
14function blogSearchMSN2(oInputId) {
15 var oInput = document.getElementById(oInputId);
16 top.location = 'http://search.msn.com/advresults.aspx?q=' + escape(oInput.value) + '%20cumpsd&FORM=SMCA&adv_f=all&adv_dom=weblogs.asp.net&adv_depth=&adv_dt=html&adv_dt=pdf&adv_dt=ppt&adv_dt=msword&adv_dt=x';
17 return false;
18}
19</script>
Update: Fixed script to work with Firefox as well (removed window.navigate and fixed event parameter and keyCode)
Update2: Added escape(), MSN version and site: restriction
Update3: Fixed MSN version, had a typo in the function name and id
Update4: Added important notice about Google terms. (This does mean I have informed you of it and you don't have to comment here about legal issues...)
Filed under: ,

Comments

# Searching Your Blog

Tuesday, January 11, 2005 11:05 AM by TrackBack

# Just added search to my site

Tuesday, January 11, 2005 12:47 PM by TrackBack

# Search my blog

Tuesday, January 11, 2005 1:42 PM by TrackBack

# re: Searching my blog

Tuesday, January 11, 2005 2:52 PM by TrackBack

# Google Your Own Blog

Tuesday, January 11, 2005 2:56 PM by TrackBack

# Add Search to .Text

Tuesday, January 11, 2005 3:12 PM by TrackBack

# Adding Search to my Blog

Tuesday, January 11, 2005 4:21 PM by TrackBack

# re: Adding Search to my Blog

Tuesday, January 11, 2005 4:22 PM by Matt Hawley

Nice post, but your own search doesn't work - at least in FireFox.

# re: Adding Search to my Blog

Tuesday, January 11, 2005 4:39 PM by David Cumps

Now it works ;)

# re: Adding Search to my Blog

Tuesday, January 11, 2005 4:53 PM by Rob Chartier


Works awesome.

Also note that I changed the google url to .COM instead of .BE for the standard English site.

Thanks.

# re: Adding Search to my Blog

Tuesday, January 11, 2005 5:04 PM by Matt Hawley

Awesome, added it to my blog too :) Thanks for sharing!

# Added Search and MS Alerts to my Blog

Tuesday, January 11, 2005 6:14 PM by TrackBack

# Add a google search to your blog

Tuesday, January 11, 2005 7:10 PM by TrackBack

# Adding Search to my Blog

Tuesday, January 11, 2005 8:23 PM by TrackBack

# re: Adding Search to my Blog

Tuesday, January 11, 2005 8:23 PM by Heather

David...you are a rock star! Once I figured out exactly what to paste, it worked great! THANK YOU!

This might be a stupid question, but if I replace the google.com/search with MSN's search site, would it work for MSN also? Don't laugh at me for not knowing this ; ) I'm sure that some of the code would be the same..would I have to change anything else?

# re: Adding Search to my Blog

Tuesday, January 11, 2005 8:29 PM by Anatoly Lubarsky

Heather: for MSN it will be
search.msn.com instead of www.google.com and
search.yahoo.com for yahoo

# re: Adding Search to my Blog

Tuesday, January 11, 2005 8:31 PM by Anatoly Lubarsky

David, some enhancements:
you should use escape(oInput.value), since your search will not work for queries like c# or alike and you can use window.open instead of top.location, it would be better I think

# re: Adding Search to my Blog

Tuesday, January 11, 2005 8:43 PM by Heather

Anatoly...I tried that and it still searched in google..which I thought was really weird. I pasted it into the same place (static news). Is there something I should insert between them? I don't know why it would still search google even after I put MSN in there.

Here's what I used (Sorry David for clogging up your comments section):

<h3>Search my Blog Using MSN</h3>
<input class="BlogSearch" type="text" name="searchBox" id="blogSearchText" value="" onkeypress="return blogSearch(event, this);">
<input type="button" value="Search" onclick="return blogSearch2('blogSearchText');" class="BlogSearchButton">
<script type="text/javascript">

function blogSearch(event, oInput) {
var keyCode = (event) ? event.keyCode : keyStroke.which;
if (keyCode == 13) {
top.location = 'http://www.search.msn.com/search?q=">http://www.search.msn.com/search?q=' + oInput.value + '+inurl%3heatherleigh+%3Aweblogs.asp.net';
return false;
} return true;
}

function blogSearch2(oInputId) {
var oInput = document.getElementById(oInputId);
top.location = 'http://www.search.msn.com/search?q=">http://www.search.msn.com/search?q=' + oInput.value + '+inurl%3Aheatherleigh+%3Aweblogs.asp.net';
return false;
}
</script>


Is there something I should change? I can't believe I am asking for coding advice..I might have to bookmark this!

# Added Search to my blog

Tuesday, January 11, 2005 11:27 PM by TrackBack

# Adding Search to your .Text Blog

Wednesday, January 12, 2005 2:11 AM by TrackBack

# Add a Search box to your blog

Wednesday, January 12, 2005 2:59 AM by TrackBack

# re: Adding Search to my Blog

Wednesday, January 12, 2005 6:08 AM by David Cumps

Thanks for your positive comments, obviously there is a need to add search to this .Text version :p

Anatoly:
I added escape(), tnx for the tip!

Heather:
I made it easy or you and added an MSN version (which you can use together with the google one if you want, the function names are different)

# re: Adding Search to my Blog

Wednesday, January 12, 2005 7:00 AM by Dennis v/d Stelt

No idea if I can say this, but Scott promised me last weeks of december that in january a new version of .Text would be released.

But a few days ago I got an email that it's gonna be "next couple of weeks" for the release! :)

# Search in blogs

Wednesday, January 12, 2005 7:03 AM by TrackBack

# re: Adding Search to my Blog

Wednesday, January 12, 2005 7:06 AM by David Cumps

Cool, hopefully it will run here as well :p curious to see the new features

(update: fixed MSN version, made a typo in the function name)

# re: Adding Search to my Blog

Wednesday, January 12, 2005 9:34 AM by James Geurts

You rock man! Thanks!

# Do you like reading a blog author? Retrieve all blog entries locally for reading/searching using XML, XSLT, XPATH

Wednesday, January 12, 2005 10:36 AM by TrackBack

# Add search to

Wednesday, January 12, 2005 10:38 AM by TrackBack

# re: Adding Search to my Blog

Wednesday, January 12, 2005 11:08 AM by Dhoore

nice going, rock star ;)

# Add search to .Text Blogs!

Wednesday, January 12, 2005 11:47 AM by TrackBack

# re: Adding Search to my Blog

Wednesday, January 12, 2005 6:28 PM by AT

IANAL,
But it looks like you have violated section "2.3 Attribution" of Google Free search argeement at http://www.google.com/services/terms_free.html by not providing a Google logo.

As well you have violated section "1.5 Exclusivity" by offering a link to MSN search on same website.


IMHO, The best way to use Google is to plug in this code:
<FORM method="GET" action="http://www.google.com/search">
<INPUT TYPE="text" name="q" size="25" maxlength="255" value="">
<INPUT TYPE="hidden" name="as_q" value="inurl:/heatherleigh">
<INPUT TYPE="hidden" name="as_sitesearch" value="blogs.msdn.com">
<INPUT TYPE="submit" name="btnG" VALUE="Search in blog">
</FORM>
<a href="http://www.google.com">
<IMG SRC="http://www.google.com/logos/Logo_25wht.gif" border="0" ALT="Powered by Google">
</a>

Take a look - different q and as_q params used ;-)

The only (but huge for me) problem is nested forms due to global ASP.NET form one.
Possibly some kind of ASP or CSS trick requered :-(
Anybody can workaround nested forms problem in ASP without javascript ? I'm not an ASP person :-(

For all others who does not use ASP - and can reallign <form>'s in their templates - my solution will be acceptable.

# re: Adding Search to my Blog

Wednesday, January 12, 2005 7:14 PM by David Cumps

Problem solved, no Google search then for now... I don't want any of those logos disrupting my layout. No visitors for Google then from here.

CSS tricks to change form logic? you have read too many service agreements and not enough code :p

Just because there is the form 'problem' with ASP.NET the solution is Javascript (as you can read on the blog I linked)

PS: Now I really hope there will be an integrated .Text search soon, just to get rid of 'violating' agreements.

PS2: Ok, I'll make a google graphic myself, as that is allowed by their agreement, just not an official graphic ;)

# re: Adding Search to my Blog

Wednesday, January 12, 2005 7:26 PM by David Cumps

Guess what, now i'm ok with the Attribution and Exclusivity sections of Google's agreement :p And my site is still small-style ;)

# Adding Search to my blog the MSN Search Beta way

Thursday, January 13, 2005 2:56 AM by TrackBack

# Google Your Own Blog

Thursday, January 13, 2005 7:40 AM by TrackBack

# re: Adding Search to my Blog

Thursday, January 13, 2005 9:12 AM by AT

Forgive me. Do not take this personaly - but you are again in trouble,

Take a look on this page:
http://www.google.com/stickers.html

And take a read

http://www.google.com/permissions/guidelines.html
"One of the conditions for all uses is that you can't mess around with our logo. Only we get to do that. "

"If you are using the Google logo on a web page, there must exist a minimum spacing of 25 pixels between each side of the logo and other graphic or textual elements on your web page"

Probably the best way to is to add "Powered By" logo at the footer of your page near .TEXT and ASP.NET logos.
You are not allowed to play with logos as you wish - there are strict rules :-(

BTW, Goggle is not lonely in regulating all uses of their products. For Microsoft there are huge dedicated website with details with Do and Don't at http://www.microsoft.com/permission/
In the same time - there are rules for MSN search box (they do not allow to play with box code - but does not requere Exclusivity) http://beta.search.msn.com/docs/affiltou.aspx


For Intel - http://intel.com/sites/corporate/tradmarx.htm
Even Redhat has strict rules about trademarks and all communications at http://www.redhat.com/legal/legal_statement.html

Doing software/web-site development is not technical-only job. You need to play fair on legal side too and respect permissions that others people grant to you.

# re: Adding Search to my Blog

Thursday, January 13, 2005 10:31 AM by David Cumps

No more search then, let's wait for the integrated search.

# Add Searching to your .TEXT blog

Thursday, January 13, 2005 11:07 AM by TrackBack

# re: Adding Search to my Blog

Thursday, January 13, 2005 1:36 PM by Dennis v/d Stelt

Seriously? I think Google is full of shit on the guidelines. Same goes for the NedStat picture, you can't change it. But I've never had any difficulties. And if Google starts complaining, I'll remove it.

But for now I'm keeping it...

# Due to requests I added a Search feature to the blog

Friday, January 14, 2005 4:50 PM by TrackBack

# re: Adding Search to my Blog

Friday, January 14, 2005 7:23 PM by gretchen

David - Thanks for taking the time to write this. Very, very, very useful and helpful!!

# re: Coming soon...

Saturday, January 15, 2005 11:37 AM by TrackBack

# Search my Blog

Sunday, January 16, 2005 11:08 PM by TrackBack

# We've added a bit of

Monday, January 17, 2005 8:14 PM by TrackBack

# Search Added

Tuesday, January 18, 2005 10:53 AM by TrackBack

# Search Evan's Blog

Tuesday, January 18, 2005 11:54 AM by TrackBack

# search added

Wednesday, January 19, 2005 9:42 AM by TrackBack

# Search my archives

Wednesday, January 19, 2005 11:21 AM by TrackBack

# re: Adding Search to my Blog

Wednesday, January 19, 2005 8:16 PM by jayson knight

AT nailed it; before you guys all run out and install this code on your site, read my post here: http://zerotrilogy.gotdns.com/jaysonblog/archive/2005/01/18/795.aspx

I lasted all of 4 days before I simply disappeared from the Google index, and it's b/c of the search box. That being said, it's a great idea (thanks David!), but I really wish you'd pull the code from your blog as inevetibaly it could lead to a lot of unhappy bloggers who get banned from Google. I'm not as bitter as I could be, but I'm not entirely thrilled either. It could be many weeks before my site is re-spidered.

Cheers
jayson

# Removed search for now

Thursday, January 20, 2005 1:19 AM by TrackBack

# Update on Searching this site

Thursday, January 20, 2005 2:58 AM by TrackBack

# re: Adding Search to my Blog

Thursday, January 20, 2005 4:29 AM by David Cumps

Jayson, I tested some other people who used it right after this post, and their search results still work apparently. Why yours is out of the index, I have no idea.

But I have added a notice to the post above explaining it, and providing choice. Now, people reading these blogs are mature enough to decide for themselves :p

# re: Adding Search to my Blog

Thursday, January 20, 2005 4:30 AM by David Cumps

Jayson, I tested some other people who used it right after this post, and their search results still work apparently. Why yours is out of the index, I have no idea.

But I have added a notice to the post above explaining it, and providing choice. Now, people reading these blogs are mature enough to decide for themselves with this new info.

I'll go round and post a comment to the sites that are in my comments, informing them, so they can decide as well.

# re: Adding Search to my Blog

Thursday, January 20, 2005 4:53 AM by Michael Kaplan

I took the google search off of my site. Geez -- all they had to do was ask in a piece of mail. Threats and demands? Forget it....

Thanks for the heads up, in any case.

# re: Search my archives

Thursday, January 20, 2005 6:54 AM by TrackBack

# Ajout d'une recherche dans le Blog

Thursday, January 20, 2005 10:00 AM by TrackBack

# We just added the Exchange Team blog search.

Thursday, January 20, 2005 11:30 AM by TrackBack

# re: Adding Search to my Blog

Thursday, January 20, 2005 3:47 PM by jayson knight

David,

Thanks for the info. Just to make it clear, I wasn't complaining per se (as it's indeed up to me as the user of Google to read over their terms...but who has time to do that ;-)), it's just a PITA to have been dropped from the index. I've put an email into Google to try and get some more insite as to exactly what happened, I'll share whatever info I get from them with you if you'd like.

By the time I realized I needed to include the Google logo, it was already too late and that's where I get a little peeved...I wish I had enough information going in before using your code to know that I mighta been dropped. But hey, it's not the end of the world right? Just consider me a beta tester!

cheers
jayson

# re: Adding Search to my Blog

Thursday, January 20, 2005 3:53 PM by David Cumps

Sure, inform us, it's really weird they dropped you.

It took me a while as well to go to the 'legal process', as it's because of the comments I got to know about it, otherwise everything would still be without warning :p

Even a small Google logo isn't allowed :(

Tnx for beta testing ;)

# How to add search functionality to your dotnet.org.za blog

Thursday, January 20, 2005 8:56 PM by TrackBack

# re: Adding Search to my Blog

Friday, January 21, 2005 7:24 AM by Rocky Moore

Thanks posting the notice. I remove the search boxes and will look to a built in search instead.

It is scary how powerful a company like Google can be when they have control of so much of the web, that is the power to send people or not...

Thanks again!

Rocky Moore
www.HintsAndTips.com
www.RockyMoore.com

# re: Adding Search to my Blog

Saturday, January 22, 2005 4:33 AM by Bob

What I find interesting is that you are blogging from a Microsoft owned domain they use for developers to blog from, mostly employees, but not all, as you are not an MS employee, Microsoft who is rightfully very touchy about people breaking their TOS or their EULAS and you are recommending to people that they go ahead and break Googles TOS.

# re: Adding Search to my Blog

Saturday, January 22, 2005 5:17 AM by David Cumps

If you would read correctly I am NOT recommending anyone to break them, I have even suggested to remove the search from the blog altogether.

You guys already made your point about legal issues, I made a notice, don't keep coming back on it.

# Search Evan's Blog

Wednesday, January 26, 2005 7:01 AM by TrackBack

# Don't Piss Google Off

Wednesday, January 26, 2005 1:05 PM by TrackBack

# Removed search for now

Friday, January 28, 2005 4:25 AM by TrackBack

# re: Adding Search to my Blog

Saturday, February 05, 2005 9:52 PM by David Starr

Here is a different take on the problem using the Google API Web Service.

http://www.elegantcode.com/dottextweb/articles/462.aspx

# Search Added

Friday, March 11, 2005 9:33 AM by TrackBack

# Lehk

Tuesday, March 22, 2005 11:49 PM by TrackBack

# Lehk

Tuesday, March 22, 2005 11:51 PM by TrackBack

# Adding Search to your Blog

Friday, April 29, 2005 12:19 PM by TrackBack

# Adding Search to your Blog

Friday, April 29, 2005 12:20 PM by TrackBack