Adding Search to my Blog

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...)

14 Comments

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


  • Works awesome.



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



    Thanks.

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

  • Heather: for MSN it will be

    search.msn.com instead of www.google.com and

    search.yahoo.com for yahoo

  • 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



  • 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):



    &lt;h3&gt;Search my Blog Using MSN&lt;/h3&gt;

    &lt;input class=&quot;BlogSearch&quot; type=&quot;text&quot; name=&quot;searchBox&quot; id=&quot;blogSearchText&quot; value=&quot;&quot; onkeypress=&quot;return blogSearch(event, this);&quot;&gt;

    &lt;input type=&quot;button&quot; value=&quot;Search&quot; onclick=&quot;return blogSearch2('blogSearchText');&quot; class=&quot;BlogSearchButton&quot;&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;



    function blogSearch(event, oInput) {

    var keyCode = (event) ? event.keyCode : keyStroke.which;

    if (keyCode == 13) {

    top.location = '<a target="_new" href="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 = '<a target="_new" href="http://www.search.msn.com/search?q=">http://www.search.msn.com/search?q=' + oInput.value + '+inurl%3Aheatherleigh+%3Aweblogs.asp.net';

    return false;

    }

    &lt;/script&gt;





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

  • 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 &quot;next couple of weeks&quot; for the release! :)

  • You rock man! Thanks!

  • nice going, rock star ;)

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

  • 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.

  • 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

  • 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

  • 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.

Comments have been disabled for this content.