如何在我的网站上添加Google搜索框?

wah*_*509 20 html google-search

我正在尝试将Google搜索框添加到我自己的网站.我想搜索谷歌本身,而不是我的网站.我有一些用于工作的代码,但不再适用:

<form method="get" action="https://www.google.com/search">
<input type="text" name="g" size="31" value="">
</form>
Run Code Online (Sandbox Code Playgroud)

当我尝试进行搜索时,它会直接转到Google主页.好吧,实际上它指向这里:https://www.google.com/webhp

有没有人有不同的解决方案?我究竟做错了什么?

Cry*_*hic 20

很抱歉回复一个较旧的问题,但我想澄清最后一个问题.

您为表单使用"get"方法.当输入字段的名称为"g"时,它将生成如下URL:

https://www.google.com/search?g=[value from input-field]
Run Code Online (Sandbox Code Playgroud)

但是当您使用Google搜索时,您会注意到以下网址:

https://www.google.nl/search?q=google+search+bar
Run Code Online (Sandbox Code Playgroud)

Google使用"q"Querystring变量作为搜索查询.因此,将您的字段从"g"重命名为"q"解决了这个问题.


san*_*san 12

这是将Google网站搜索添加到网站的方法之一:

<form action="https://www.google.com/search" class="searchform" method="get" name="searchform" target="_blank">
<input name="sitesearch" type="hidden" value="example.com">
<input autocomplete="on" class="form-control search" name="q" placeholder="Search in example.com" required="required"  type="text">
<button class="button" type="submit">Search</button>
</form>
Run Code Online (Sandbox Code Playgroud)


wah*_*509 1

想通了,伙计们!对于文本框的名称,您必须使用“q”。我用“g”只是为了我个人的喜好。但显然它必须是“q”。

有人知道为什么吗?

  • `q` 代表“查询”,这是搜索数据库的常用术语。 (3认同)