Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:04:58+00:00 2026-06-04T06:04:58+00:00

Getting some odd behavior from google custom search that I can’t seem to suss

  • 0

Getting some odd behavior from google custom search that I can’t seem to suss out. Maybe someone has a clue.

I’m putting together a Magento site, which has its own internal search engine – but is limited to product only. I want to implement google custom search results on the search results page as well. I figure I should be able to simply execute a search based on the query vars in the url (to return all the non-product content), as such:

        <section style="min-height:600px">
            <div style="background-color:#DFDFDF; min-height:800px; width:100%;">
                <div id="cse">Loading</div>
            </div>
        <script src="http://www.google.com/jsapi" type="text/javascript"></script>
        <script type="text/javascript"> 
        //<![CDATA[

            $(document).ready( function(){ 
                console.log('search initiated');
                var t = window.setTimeout( function(){ customSearch(); }, 5000 );
            });

            function customSearch(){
                var q = urlParams()['q'];
                if (q != undefined && q != ""){
                    console.log('q : %s', q); //outputs successfully

                    google.load('search', '1');
                    google.setOnLoadCallback(function () {
                        var customSearchControl = new google.search.CustomSearchControl(MY CUSTOM ID KEY);
                        var cseDrawOptions = new google.search.DrawOptions();
                        cseDrawOptions.setAutoComplete(true); //unknown if this is required... 
                        customSearchControl.draw('cse',cseDrawOptions);                    
                        customSearchControl.execute(q);

                    }, true);

                } 
            }

           function urlParams(){
                    var vars = []; 
                    var hash;
                    var index = window.location.href.indexOf('?');
                    if( index != -1 ){
                        var hashes = window.location.href.slice(index + 1).split('&');
                        for(var i = 0; i < hashes.length; i++){
                            hash = hashes[i].split('=');
                            vars.push(hash[0]);
                            vars[hash[0]] = hash[1].replace(/\+/g, " ");
                        }
                    }
                    return vars;
                }

        //]>
        </script>
        </section>

I’ll note that I’ve pulled all other content out of the logic (but its implementation in magento is identical).

So the behavior goes like this: page loads fine (I’m delaying the google search with a timeout for testing purposes ). Assuming there is a query var in the url the console traces out as expected. Then the page just gets wiped out, with no content back from google. “Wiped out”… meaning all elements on teh page disappear, or are getting overwritten by a new page that google loads. As if the search control isn’t creating an iframe – its just replacing the page with a <body>-less html page.

I’ve ready a number of articles on the subject, and gone over the API – this code looks like it should work. But clearly isn’t.

What am I missing?

Cheers –

UPDATE

Continued messing around with this has revealed that for whatever reason :

google.load('search', '1');
google.google.setOnLoadCallback( console.log('loaded') )

Was the cause of the replaced page issue. The responded page, however contained links to the search module that google is hosting. And if I manually linked those files (forgoing a google.load) then I could run a search as expected:

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script src="http://www.google.com/uds/?file=search&v=1" type="text/javascript"></script>
<script type="text/javascript"> 
//<![CDATA[
  ... search logic

Then I found an alternate syntax on the google developers page that seemed to work as expected:

$(document).ready( function(){ 
        google.load("search", "1", {"callback" : customSearch});
    });

    function customSearch(){
        var q = urlParams()['q'];
        if (q != undefined && q != ""){
            var cseControl = new google.search.CustomSearchControl('MY CUSTOM KEY');

            var cseDrawOptions = new google.search.DrawOptions();
            cseDrawOptions.enableSearchResultsOnly()
            cseControl.draw('cse', cseDrawOptions);
            cseControl.execute(q);
        } 
    }

Which works as expected. Only real problem at this point is the host of

Unsafe JavaScript attempt to access frame with URL http://mydomain from frame with URL http://www.google/cse?...

That now gets thrown.

I don’t know how the two different versions of load syntax changes anything… but it seemed to of. Whatever the case, I’m unclear as to how to resolve these cross domain errors.

Thoughts would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:04:59+00:00Added an answer on June 4, 2026 at 6:04 am

    Nothin huh?

    Well – I’ve basically worked out a good solution, using an alternate method that I think will be more flexible in the long run. Using googles RESTful API and simple jquery .ajax call, I can obtain good, controllable results with no cross-domain errors:

    <div id="cse">Loading</div>
    <script>
        //https://developers.google.com/custom-search/v1/getting_started
        //https://developers.google.com/custom-search/v1/using_rest#query-params
        //https://developers.google.com/custom-search/v1/cse/list
    
        var _url    = "https://www.googleapis.com/customsearch/v1";
        var _key    = 'AIzaSy... your api key here'; 
        var _cx     = '001809... your engine id';
        var _q      = urlParams()['q'];                         //query param
    
        jQuery(document).ready(function() {
    
            $j.ajax({
                url     : _url,
                type    : 'GET',
                dataType : 'jsonp',
                data :{
                    key : _key,
                    cx  : _cx,
                    q   :_q 
                },
                success     : function(data, textStatus, jqXHR){ responseHandler(data); },
                error       : function(jqXHR, textStatus, errorThrown){ console.log('error: %s'), errorThrown},
                beforeSend  : function(){ console.log('sending request')},
                crossDomain : true
            });
    
        });
    
        function responseHandler( response, status) {
            console.log(response);
    
            var cse = $j('#cse');  // render vars as needed...
            for (var i = 0; i < response.items.length; i++) {
                var item = response.items[i];
                cse.append( "<br>" + item.htmlTitle);
            }
        }
    
        function urlParams(){
            var vars = []; 
            var hash;
            var index = window.location.href.indexOf('?');
            if( index != -1 ){
                var hashes = window.location.href.slice(index + 1).split('&');
                for(var i = 0; i < hashes.length; i++){
                    hash = hashes[i].split('=');
                    vars.push(hash[0]);
                    vars[hash[0]] = hash[1];
                }
            }
            return vars;
        }
    
    </script>
    

    And you can too;D

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting some very odd behavior and I can't figure it out. I've modified
I am getting some behavior from a LINQ Where clause that I can't quite
I'm getting some fairly odd behavior here... I noticed that only localhost, a header
I'm getting some odd behaviour with my custom tab bar. The images seem to
I'm getting some weird behavior from IE when trying to change a session variable
css/hmtl newbie here. I've been messing with divs and I'm getting some odd behavior
I've been getting some odd behavior using a foreach today. I have a dataset
I was getting some odd behaviour out of a switch block today, specifically I
I am getting some odd results when trying to run a process from a
I'm getting some odd behavior in one of my class members and it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.