Note that there are some explanatory texts on larger screens.

plurals
  1. POCall function after loading google custom search results?
    text
    copied!<p>I basically need to run some jQuery code after the search results get rendered on my page. I can use either the v1 code:</p> <pre><code>&lt;div id="cse" style="width: 100%;"&gt;Loading&lt;/div&gt; &lt;script src="http://www.google.com/jsapi" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; google.load('search', '1', {language : 'en', style : google.loader.themes.V2_DEFAULT}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var orderByOptions = {}; orderByOptions['keys'] = [{label: 'Relevance', key: ''},{label: 'Date', key: 'date'}]; customSearchOptions['enableOrderBy'] = true; customSearchOptions['orderByOptions'] = orderByOptions; var customSearchControl = new google.search.CustomSearchControl( 'zzzzzzzzzzzz', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.setAutoComplete(true); customSearchControl.setAutoCompletionId('zzzzzz:zzzzzzz+qptype:3'); options.enableSearchResultsOnly(); customSearchControl.draw('cse', options); function parseParamsFromUrl() { var params = {}; var parts = window.location.search.substr(1).split('\x26'); for (var i = 0; i &lt; 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); &lt;/script&gt; </code></pre> <p>Or the v2 code:</p> <pre><code>&lt;!-- Put the following javascript before the closing &lt;/head&gt; tag. --&gt; &lt;script&gt; (function() { var cx = 'xxxxxxxxx'; var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true; gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s); })(); &lt;/script&gt; </code></pre> <p> </p> <p>I've tried putting my function call in various places in the code but no avail.</p> <p>I have a function that needs to dynamically set the height of the container the search results appear in, so once the results load I need to call it otherwise my page displays incorrectly.</p> <p>Here is a link to their v2 API documentation: <a href="https://developers.google.com/custom-search/docs/element#cse-element">https://developers.google.com/custom-search/docs/element#cse-element</a> and v1: <a href="https://developers.google.com/custom-search/docs/js/cselement-reference">https://developers.google.com/custom-search/docs/js/cselement-reference</a> . I can't see anything where there is a callback upon search result rendering, only on initialization. Seems crazy though there isn't support for something like this.</p> <p>Here is what I've tried with v2:</p> <pre><code>(function () { var cx = 'xxxxxxxxx'; var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true; gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx; gcse.onreadystatechange = gcse.onload = function () { console.log("executed"); SetMainHeight(20); }; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s); })(); </code></pre> <p>The console never outputs "executed". Also I notice in my console I have the following error:</p> <blockquote> <p>Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL <a href="http://www.google.com/cse?q=test&amp;client=google-coop&amp;hl=en&amp;r=s&amp;cx=xxxxx">http://www.google.com/cse?q=test&amp;client=google-coop&amp;hl=en&amp;r=s&amp;cx=xxxxx</a>…s1%2Csr1&amp;rurl=http%3A%2F%2Flocalhost%3A58257%2FSearch%3Fq%3Dtest#slave-1-1. Domains, protocols and ports must match.</p> </blockquote> <p>Not sure if that matters as the search function still works fine. I am testing this on localhost over http.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload