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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13822927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:19:22  来源:igfitidea点击:

How can I add a Google search box to my website?

htmlgoogle-search

提问by wahle509

I am trying to add a Google search box to my own website. I would like it to search Google itself, not my site. There was some code I had that use to work, but no longer does:

我正在尝试将 Google 搜索框添加到我自己的网站。我希望它搜索 Google 本身,而不是我的网站。有一些代码我曾经用来工作,但不再起作用:

<form method="get" action="https://www.google.com/search">
<input type="text" name="g" size="31" value="">
</form>

When I try making a search, it just directs to the Google homepage. Well, actually it directs here: https://www.google.com/webhp

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

Does anyone have a different solution? What am I doing wrong?

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

回答by Cryothic

Sorry for replying on an older question, but I would like to clarify the last question.

很抱歉回答了一个较旧的问题,但我想澄清最后一个问题。

You use a "get" method for your form. When the name of your input-field is "g", it will make a URL like this:

您对表单使用“get”方法。当您的输入字段的名称为“g”时,它将生成如下所示的 URL:

https://www.google.com/search?g=[value from input-field]

But when you search with google, you notice the following URL:

但是当您使用 google 搜索时,您会注意到以下 URL:

https://www.google.nl/search?q=google+search+bar

Google uses the "q" Querystring variable as it's search-query. Therefor, renaming your field from "g" to "q" solved the problem.

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

回答by sansan

This is one of the way to add google site search to websites:

这是将 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>

回答by Yusubov

There are couple of tricks that may help you. I guess, you are missing some configurations while using the Google code.

有几个技巧可以帮助你。我想,您在使用 Google 代码时缺少一些配置。

Look at these useful links:

看看这些有用的链接:

回答by wahle509

Figured it out, folks! for the NAME of the text box, you have to use "q". I had "g" just for my own personal preferences. But apparently it has to be "q".

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

Anyone know why?

有谁知道为什么?

回答by Kwilver Bear

No need to embed! Just simply send the user to google and add the var in the search like this: (Remember, code might not work on this, so try in a browser if it doesn't.) Hope it works!

无需嵌入!只需简单地将用户发送到 google 并在搜索中添加 var,如下所示:(请记住,代码可能无法解决此问题,因此如果不能,请在浏览器中尝试。)希望它有效!

<textarea id="Blah"></textarea><button onclick="search()">Search</button>
<script>
function search() {
var Blah = document.getElementById("Blah").value;
location.replace("https://www.google.com/search?q=" + Blah + "");
}
</script>

    function search() {
    var Blah = document.getElementById("Blah").value;
    location.replace("https://www.google.com/search?q=" + Blah + "");
    }
<textarea id="Blah"></textarea><button onclick="search()">Search</button>