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

后端 未结 4 1546
无人共我
无人共我 2020-12-02 17:10

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 doe

相关标签:
4条回答
  • 2020-12-02 17:52

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

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

    0 讨论(0)
  • 2020-12-02 17:57

    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!

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

    0 讨论(0)
  • 2020-12-02 18:04

    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:

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

    But when you search with google, you notice the following 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.

    0 讨论(0)
  • 2020-12-02 18:14

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

    Anyone know why?

    0 讨论(0)
提交回复
热议问题