How to show next/previous links in Google Custom Search Engine paging links

爷,独闯天下 提交于 2019-12-04 13:46:12

I stepped through the javascript and found the undocumented properties I was looking for.

<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'});
google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('GOOGLEIDGOESHERE');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    customSearchControl.setSearchCompleteCallback(null, 
        function() { searchCompleteCallback(customSearchControl) });

    customSearchControl.draw('cse');   
}, true);


function searchCompleteCallback(customSearchControl) {

    var currentPageIndex = customSearchControl.e[0].g.cursor.currentPageIndex;

    if (currentPageIndex < customSearchControl.e[0].g.cursor.pages.length - 1) {
        $('#cse .gsc-cursor').append('<div class="gsc-cursor-page">Next</div>').click(function() {
            customSearchControl.e[0].g.gotoPage(currentPageIndex + 1);
        });
    }

    if (currentPageIndex > 0) {
        $($('#cse .gsc-cursor').prepend('<div class="gsc-cursor-page">Previous</div>').children()[0]).click(function() {
            customSearchControl.e[0].g.gotoPage(currentPageIndex - 1);
        });
    }

    window.scrollTo(0, 0);

}
</script>

<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />

I've been using this to find the current page:

ctrl.setSearchCompleteCallback(null, function(gControl, gResults)
{
    currentpage = 1+gResults.cursor.currentPageIndex;
    // or, here is an alternate way
    currentpage = $('.gsc-cursor-current-page').text();
});
Bart

And now it's customSearchControl.k[0].g.cursor ... (as of this weekend, it seems)

Next time it stops working just go to script debugging in IE, add customSearchControl as a watch, open the properties (+), under the Type column look for Object, (Array) and make sure there is a (+) there as well (i.e. contains elements), open[0], and look for Type Object, again with child elements. Open that and once you see "cursor" in the list, you've got it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!