Add Google search in web page

17,600

You just need to set the form method to "get", add one extra hidden element with the site you want to search and it will automatically paste it behind the URL as such:

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

Because that is how HTML forms work by default.

Share:
17,600
Gulbahar
Author by

Gulbahar

Updated on July 28, 2022

Comments

  • Gulbahar
    Gulbahar almost 2 years

    I'd like to add search to a static site. The simplest way is to simply query Google by appending "site:www.acme.com" to the actual query so that Google will limit search to that site.

    Ideally, I'd like to do this in the browser, to avoid having to install PHP on the server. Using a form, I don't know how to append the search items:

    <form action=http://www.google.com?q="site:www.acme.com+...">
    <input type=text id=search_item>
    </form>
    

    Does someone know of a client-side solution? Should I use some JavaScript for this?

    Thank you.


    Edit: When using "method=get" and "input name=q value="site:www.acme.com "", the browser will indeed call Google with "www.google.com?q="site:www.acme.com some item", but I'd rather avoid presetting the input box with "site:www.acme.com" because users will find this odd and might remove it.

  • Gulbahar
    Gulbahar over 11 years
    Thanks for the code. RR's solution works without using JavaScript.
  • Quentin
    Quentin about 7 years
    "You just need to set the form method to "get"" — That isn't needed. GET is the default method.
  • Quentin
    Quentin about 7 years
    That searches the whole WWW. The question is about searching a specific site.
  • RadicalRaid
    RadicalRaid about 7 years
    I added it explicitly (about five years ago) to make sure it's clear because this might not work when posting.
  • Ahsan Habib
    Ahsan Habib about 6 years
    I tried this but not working. <form action="google.com/search" method="get"> <input type="hidden" name="sitesearch" value="emoncmt2.byethost15.com/Digital" /> <input type="text" name="q" /> </form>