Search code examples
javascripthtmlgoogle-custom-search

Executing default query in google custom search engine V2 code


I want to automatically execute default query in google custom search engine when the page loads. I am using v2 code of cse.

when the page loads the default should be executed automatically showing results only. How can we do it?


Solution

  • You can pass a q param in your url:

    http://example.com/?q=some_query

    More difficult is using callback after the script is loaded: https://developers.google.com/custom-search/docs/element#javascript

    var myCallback = function() {
        google.search.cse.element.render();
        google.search.cse.element.getElement('my_search_el').execute("some_query");
    };
    

    And in html:

    <gcse:search gname="my_search_el"></gcse:search>