【问题标题】:Loading Google Custom Search results without page refresh在不刷新页面的情况下加载 Google 自定义搜索结果
【发布时间】:2012-04-05 02:07:03
【问题描述】:

我想提交一个谷歌自定义搜索查询而不重新加载/刷新整个 html 页面。我正在使用带有“仅结果”布局的最新 v2 gcs。

在搜索表单上方的任何位置加载 gcs api

<script src="//www.google.com/jsapi" type="text/javascript"></script>
<script>
    google.load('search', '1',
        {language : 'en', style : google.loader.themes.V2_DEFAULT});
</script>

我的自定义搜索表单

<form onsubmit="return executeQuery();" id="cse-search-box-form-id">
    <input type="text" name="q" id="cse-search-input-box-id" size="25" autocomplete="off"/>
    <input type="submit" id="site-search-submit" value="search"/>
</form>

gcs 结果脚本放置在需要搜索结果的任何位置

<div id="cse" style="width: 100%;">Loading</div>

<script src="http://www.google.com/jsapi" type="text/javascript"></script>

<script type="text/javascript"> 

    google.load('search', '1', {language : 'en', style : google.loader.themes.V2_DEFAULT});
    google.setOnLoadCallback(function() {
        var customSearchOptions = {};  
        var customSearchControl = new google.search.CustomSearchControl(
       'UNIQUE-API-KEY', customSearchOptions);
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
        var options = new google.search.DrawOptions();
        options.setAutoComplete(true);
        options.enableSearchResultsOnly(); 
        customSearchControl.draw('cse', options);
        function parseParamsFromUrl() {
            var params = {};
            var parts = window.location.search.substr(1).split('\x26');
            for (var i = 0; i < parts.length; i++) {
                var keyValuePair = parts[i].split('=');
                var key = decodeURIComponent(keyValuePair[0]);
                params[key] = keyValuePair[1] ?
                    decodeURIComponent(keyValuePair[1].replace(/\+/g, ' ')) :
                    keyValuePair[1];

            }

            return params;

        }

        var urlParams = parseParamsFromUrl();
        var queryParamName = "q";
        if (urlParams[queryParamName]) {
            customSearchControl.execute(urlParams[queryParamName]);

        }

    }, true);

</script>

感谢任何帮助。

谢谢

更新

我已经实现了,

customSearchControl.execute(urlParams[queryParamName]);

现在我的搜索表单如下:

<form onsubmit="customSearchControl.execute(urlParams[queryParamName]);" id="cse-search-box-form-id">
    <input type="text" name="q" id="cse-search-input-box-id" size="25" autocomplete="off"/>
    <input type="submit" id="site-search-submit" value="search"/>
</form>

但是,执行搜索仍会刷新整个页面,这会使我的初始 html 格式在 jquery 脚本启动之前陷入混乱。

谢谢

更新

我以多种组合添加了以下所有类型,但要么刷新整个页面,要么根本没有任何反应。

<form onsubmit="return executeQuery(); return false;" id="cse-search-box-form-id">

<form onsubmit="executeQuery(); return false;" id="cse-search-box-form-id">

<form onsubmit="return false; executeQuery();" id="cse-search-box-form-id">

<form onsubmit="return false; return executeQuery();" id="cse-search-box-form-id">

<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); return false;" id="cse-search-box-form-id">

<form onsubmit="return executeQuery(); event.preventDefault();" id="cse-search-box-form-id">

<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); event.preventDefault();" id="cse-search-box-form-id">

<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); event.stopPropagation();" id="cse-search-box-form-id">

等等……

有人有这方面的经验吗?用于进一步定制的 json api 呢?这会以某种方式解决页面刷新的问题吗?

谢谢

【问题讨论】:

    标签: ajax google-custom-search


    【解决方案1】:

    你需要打电话

    customSearchControl.execute(urlParams[queryParamName]);

    当您需要搜索时。所以在表单的 onsubmit 中使用该函数。

    在解决这个问题时,我已经在自己的网站上进行了设置。你可以check it out here

    基本上,我只是通过 jquery 将提交处理程序添加到表单,并完成了谷歌在提交处理程序中所做的所有工作。完美无缺。

    【讨论】:

    • 感谢您的回复,iamgopal。这会在不重新加载页面的情况下执行查询并更新搜索结果吗?我将如何使用 jquery .on() 提交表单?或者,还有更好的方法?谢谢。
    • 添加返回false;提交。所以它不会重新加载整个页面。
    • 我已经尝试了上述方法,但没有什么能阻止页面刷新。它正在扼杀我的设计。我有一个隐藏面板,它在搜索表单的单击或提交事件中淡入。任何一个都是无用的,因为一旦页面刷新,所有内容都会被删除,并且 jquery/javascript 必须重新启动。
    【解决方案2】:

    最好的选择是使用 JQuery 创建钩子。下面的代码将在渲染完成时在 searchButtonsearchresults-only 之间创建一个挂钩。

    为了使此设置生效,您需要为您的元素指定一个 gname,否则您将无法通过 google.cse.element 方法找到它。我有下面代码的live version,但我不承诺会永久生效,因为它托管在我的 NDSU FTP 中。

    Index.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>Google Search Customization</title>
        <script src="https://code.jquery.com/jquery-1.12.1.js"></script>
        <script src="index.js"></script>
      </head>
      <body>
        <h1>Trigger an Element API v2 search through a custom textbox</h1>
        <label>Enter Search Query:</label><input id="customSearchText" type="text">
        <input id="customSearch" type="submit">
        <div style="width: 50%; float: right;">
          <gcse:searchresults-only gname="searchOnlyCSE"></gcse:searchresults-only>
        </div>
      </body>
    </html>
    

    index.js

    //Hook a callback into the rendered Google Search. From my understanding, this is possible because the outermost rendered div has id of "___gcse_0".
    window.__gcse = {
      callback: googleCSELoaded
    }; 
    function googleCSELoaded() {
      // The hook in question.
      $("#customSearch").click(function() {
        var searchText = $("#customSearchText").val();
        console.log(searchText);
        var element = google.search.cse.element.getElement('searchOnlyCSE');
        element.execute(searchText);
      })
    }
    
    // CSE Code. This is a free version, so please don't make too many requests.
    (function() {
      var cx = '001386805071419863133:cb1vfab8b4y';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    })();
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多