Google自定义搜索
Google自定义搜索的页面申请网址是:
https://www.google.com/cse/?hl=zh-CN
登陆进去选择 < 新建搜索引擎...>
然后填写一些基本资料
设置显示效果
本站调用的方法是
侧栏的搜索代码是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <div id="cse-search-form" style="width: 100%;">Loading</div> <script src="https://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1', {language : 'zh-CN', style : google.loader.themes.MINIMALIST}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var orderByOptions = {}; orderByOptions['keys'] = [{label: '相关性', key: ''},{label: '日期', key: 'date'}]; customSearchOptions['enableOrderBy'] = true; customSearchOptions['orderByOptions'] = orderByOptions; var customSearchControl = new google.search.CustomSearchControl( '016677070822716074267:WMX-477634419', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET); var options = new google.search.DrawOptions(); options.enableSearchboxOnly("http:\x2F\x2F18air.top\x2Fsearch", ""); customSearchControl.draw('cse-search-form', options); }, true); </script> |
其中代码说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | options.enableSearchboxOnly("http:\x2F\x2F18air.top\x2Fsearch", ""); 是将搜索结果在18air.top\search页面显示 search是一个自定义页面 这个自定义页面的代码是 <div id="cse" style="width: 100%;">Loading</div> <script src="https://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1', {language : 'zh-CN', style : google.loader.themes.MINIMALIST}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var orderByOptions = {}; orderByOptions['keys'] = [{label: '相关性', key: ''},{label: '日期', key: 'date'}]; customSearchOptions['enableOrderBy'] = true; customSearchOptions['orderByOptions'] = orderByOptions; var customSearchControl = new google.search.CustomSearchControl( '016677070822716074267:WMX-477634419', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET); customSearchControl.draw('cse'); 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> </script> |
具体效果请用本站搜索