Gulbahar
Gulbahar

Reputation: 5537

Add Google search in web page

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.

Upvotes: 8

Views: 19703

Answers (3)

Dr.Darshan
Dr.Darshan

Reputation: 139

  <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 Bing Search engine then replace 'google' with 'bing'.

Upvotes: -1

RadicalRaid
RadicalRaid

Reputation: 1006

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.

Upvotes: 26

my_name_564
my_name_564

Reputation: 9

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>

Upvotes: 0

Related Questions