How do I add the stock Google search to my own website **with auto-complete**?

394 Views Asked by At

I am not asking about Site Search. I am wondering how to include a Google search box that searches the whole internet (not limited to a single site like Site Search would be) that also uses the same autocomplete behavior you see on Google.com.

Some of you may remember iGoogle, which was Google's web portal which I used and loved for years until they shut it down last year. I'm making my own iGoogle replacement. I can easily enough add a textbox up top to do the search, but what I'm having trouble reproducing is the JavaScript autocomplete behavior that you get with regular Google.com.

So starting with this:

<form action="https://www.google.com/search" method="get">
<input type="text" name="q" value="" />
</form>

How do I get it to do auto-complete?

3

There are 3 best solutions below

0
On BEST ANSWER

You will need to use some server side scripting and try to implement a solution based on suggested queries API. More can be found on this question

On the client side try make an AJAx call as the user types to the textfield. As for the autocomplete functionality, I have used jQuery UI's plugin and it seems to work pretty great.

1
On

Did you try this? https://support.google.com/customsearch/answer/2631081

On the Custom Search home page ( https://www.google.com/cse/all ), click the search engine you want.
Click Search features, and then click the Autocomplete tab.
Switch Enable autocomplete to ON.
0
On

I'm going to accept Theodore's answer as correct simply because he led me on the right path; though to be honest, he really didn't give me much and I really came up with the solution on my own here.

I discovered that if you query this URL:

https://www.google.com/s?tch=1&sclient=psy-ab&q=search+term+goes+here&cp=21

Where q is your search term and cp is the position of the cursor as you're typing, that will return a JSON response which can be parsed and converted into something usable, then integrated with jQuery UI. (Basically I just wrote a short little PHP script on my own server which opens that URL using file_get_contents.) No guarantee on how long that will work though, as it seems very unofficial.

I also ended up using the jQuery UI Autocomplete HTML extension by Scott González, because the data Google passes back has <b> tags in it already, so that worked quite well.