Monday, January 31, 2011

A simple Google Search Gadget


  For designing a new-homepage for myself(yeah I do that a lot!), I wanted a google search gadget.So, as usual I decided to make one for myself.Just a simple tweak to one of the online tutorial javascripts and I had a javascript that could be used for google search!
Here's the script that I came up with.

<script type="text/javascript">
function search()
{
var searchkey = getElementById('mytext');
if(myTextField.value != "")
window.location="http://www.google.com/search?q="+myTextField.value;
else
alert("Would you please enter some text?");
}
</script>

To complete the gadget, You need a text-box and a button.Also you need to use 'mytext' as the text-box's id and 'search()' as the onclick event for the button.
So the remaining code is:

<input type='text' id='mytext' /><br>
<input type='button' value='search' onclick='search()' />


This simple script doesn't pass all the parameters that Google does,but I am not Google, am I? This thing does the job so whats the problem? You can make the same thing for Bing by just replacing google.com with bing.com . Also you can make it site specific by using this line:

window.location="http://www.google.com/search?q="+myTextField.value+"site:www.example.com";


Enjoy scripting!

No comments:

Post a Comment