【问题标题】:Add Google search in web page在网页中添加 Google 搜索
【发布时间】:2012-11-02 23:04:00
【问题描述】:

我想将搜索添加到静态网站。最简单的方法是通过在实际查询中附加“site:www.acme.com”来简单地查询 Google,以便 Google 将搜索限制在该站点。

理想情况下,我想在浏览器中执行此操作,以避免在服务器上安装 PHP。使用表单,我不知道如何附加搜索项:

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

有人知道客户端解决方案吗?我应该为此使用一些 JavaScript 吗?

谢谢。


编辑:当使用 "method=get" 和 "input name=q value="site:www.acme.com "" 时,浏览器确实会使用 "www.google.com?q="site: www.acme.com some item”,但我宁愿避免使用“site:www.acme.com”预设输入框,因为用户会觉得这很奇怪,可能会删除它。

【问题讨论】:

    标签: html search


    【解决方案1】:

    您只需将表单方法设置为“get”,在您要搜索的站点中添加一个额外的隐藏元素,它会自动将其粘贴到 URL 后面:

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

    因为这是默认情况下 HTML 表单的工作方式。

    【讨论】:

    • "您只需将表单方法设置为 "get"" — 不需要。 GET 是默认方法。
    • 我明确添加了它(大约五年前)以确保它清楚,因为这在发布时可能不起作用。
    • 我试过这个但没有用。
      google.com/search" method="get"> emoncmt2.byethost15.com/Digital" />
    【解决方案2】:

    你可以这样做:

    <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>
    

    【讨论】:

    • 感谢您的代码。 RR 的解决方案无需使用 JavaScript。
    【解决方案3】:

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

    如果您想使用必应搜索引擎,请将“google”替换为“bing”。

    【讨论】:

    • 搜索整个 WWW。问题是关于搜索特定网站。
    猜你喜欢
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多