Add Google search on web page

I want to add a search to a static site. The easiest way is to simply query Google by adding โ€œsite: www.acme.comโ€ to the actual query so that Google limits your search on this site.

Ideally, I would like to do this in a browser to avoid having to install PHP on the server. Using the form, I do not know how to add search elements:

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

Does anyone know of a client solution? Should I use JavaScript for this?

Thanks.


Edit: when using "method = get" and "input name = q value =" site: www.acme.com "the browser will really call Google using" www.google.com?q= "site: www.acme.com which is an element ", but I would prefer to avoid pre-setting the input field with" site: www.acme.com "because users will find it strange and can delete it.

+7
source share
4 answers

You just need to set the form method to "get", add one additional hidden element with the site you want to find, and it automatically inserts it into the URL itself:

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

Since HTML forms work by default.

+17
source

u can do something like this:

 <script type="text/javascript"> function google_search() { window.location = "http://www.google.com/search?q=site:www.acme.com+" + encodeURIComponent(document.getElementById("q").value); } </script> <form onSubmit="google_search()"> <input type="text" name="q" id="q" value="" /> <input type="submit" value="search" onClick="return google_search()" /> </form> 
0
source

There is a Google service for this: Google Custom Search

-one
source

  <form method="get" action="http://google.com/search"> <input type="text" name="q" required autofocus> <input type="submit" value="Google search"> </form> 

If you want to use the Bing Search engine, replace 'google' with 'bing'.

-one
source

All Articles