I've added a Google Custom Search Engine to my site using the Google developer's console (I'm not coding the API myself). I used the two-page option and was successful in getting the search to run and display search results on the second page. However, when I click on one of the search results and then want to go back to the search results page, it goes instead to the root page of my site. I came up with a quasi-solution: when I try to go back to the search results page, the URL of the search results page is siteroot/#gsc.tab=0&gsc.q=happy%20birthday&gsc.sort=date; If I manually edit that and add the page name before the query, so it looks like this: siteroot/searchresults.cfm#gsc.tab=0&gsc.q=happy%20birthday&gsc.sort=date, then the results will load again. So I added some javascript to manually add the searchresults.cfm to the query string onload and that does (usually) work. However, if the user goes to the 2nd or 3rd page of search results or switches the sort type, it stops working. I've looked around at different sites and no one else seems to have this problem; I've looked at many sites about how to add the search engine and they all give the instructions that I followed to add this.
Any advice? I've included the code below:
<div id = "searchOuter"><script>
(function() {
var cx = 'xxxxxxxxxxxxxxxxx:xxxxxxxxxx';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s); })();</script>
<gcse:searchbox-only enablehistory = "true" resultsURL="searchResults.cfm" enableAutoComplete="true" enableOrderBy="true"></gcse:searchbox-only></div>
And on the SearchResults.cfm page:
<div> <script>
(function() {
var cx = '009457464603814366938:w0ppnk90w6m';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s); })();</script>
<gcse:search enablehistory = "true" enableAutoComplete="true" enableOrderBy="true"></gcse:search></div>
I've found an answer to my own question. Turns out that there was a bug in the CSE 'enable search results browsing history' feature. I turned this off by going into the console -> Search Features -> Advanced -> Results Browsing History and choosing 'Disable'. Then I inserted the following into the search box code on my site:enablehistory = "false"
After that, the queries stopped have the gsc.tab=0, etc appended to them, and returning to the search results page worked properly, but the links on the search results page began opening in new tabs. So I inserted the following to keep them opening in the same tab:
linktarget="_self"
. My final search box code on my site reads: <gcse:search enableAutoComplete="true" enablehistory = "false" enableOrderBy = "true" linktarget="_self"></gcse:search>
Hope this is helpful to others. Good luck!