On button click load google's first result based on search input the user has given

1.4k views Asked by At

So It's hard to write a proper title, I will do my best to explain.

The thing I'm looking for, is a simple way to Directly load the top link google present's my as if I would google a certain term.

Imagine this -> User gives the input "BMW x5" in the text box and clicks on "submit".

As a result I have an Iframe on my webpage that will have it's source set to the url that is the top result in google.com .

To make clear my Iframe will not load google.com with as search parameters "BMX x5" instead it will directly load the top listed page.

Is this possible?

1

There are 1 answers

3
mplungjan On

Have a look at this code:

https://productforums.google.com/forum/#!topic/customsearch/Iyv7_wswlqk

You need to copy this to a page of its own and target your iframe - however it is possible that google will not allow that.

If that is the case, then you need to have a proxy on your server that parses the search result and redirects to the first link

<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td colspan="2" align="center" valign="bottom">
            <form name="f" action="Your results page address" id="cse-search-box" method="get" target="_blank">
                <input type="hidden" name="cx" value="Your cx value here" />
                <input type="hidden" name="cof" value="FORID:9" />
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="text" name="q" id="q" autocomplete="off" size="51" class="txbx" />
            </form>
        </td>
    </tr>
    <tr>
        <td style="height:35px;" width="45%" align="right" valign="middle">
            <form name="s" action="Your results page address" method="get">
                <input type="hidden" name="cx" value="" />
                <input type="hidden" name="cof" value="" />
                <input type="hidden" name="ie" value="" />
                <input type="submit" name="sa" value="Search" class="btn" onClick="document.s.cx.value=document.f.cx.value; document.s.cof.value=document.f.cof.value; document.s.ie.value=document.f.ie.value;" />
            </form>
        </td>
        <td width="55%" align="left" valign="middle">
            <form name="g" action="http://www.google.co.uk/search" method="get">
                <input type="hidden" name="q" value="">
                <input name="btnI" type=submit value="I'm Feeling Lucky" class="btn" onClick="document.g.q.value=document.f.q.value;" />
            </form>
        </td>
    </tr>
</table>