www.webdeveloper.com
Results 1 to 2 of 2

Thread: google Custom Search api in a js file

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    google Custom Search api in a js file

    First I found the demo in google doc:

    HTML Code:
    <html>
      <head>
        <title>JSON/Atom Custom Search API Example</title>
      </head>
      <body>
        <div id="content"></div>
        <script>
          function hndlr(response) {
              // handle result
          }
        </script>
        <script src="https://www.googleapis.com/customsearch/v1?key=KEY&cx=cx&q=cars&callback=hndlr">
        </script>
      </body>
    </html>
    And it works fine.

    But know I want to trigger the "search process" inside a js file say mySearch.js, so how can I get this done ?

    example:

    HTML Code:
    var XXXLayer = cc.Layer.extend({
        init:function () {
            this._super();
            var theUrl = 'https://www.googleapis.com/customsearch/v1?key=KEY&cx=cx&q=cars&callback=hndlr';
    
            // what to do here ???????
    
            return true;
        },
        hndlr:function(response) {
              // handle result
        }
    });
    Any suggestion would be appreciated thanks

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    function loadScript(url, cb) {
    	var x = document.createElement("script");
    
    	function orsc(e) {
    		e = e || self.event;
    		if (e.type === "load" || {
    			loaded: 1,
    			complete: 1
    		}[x.readyState]) x.onload = x.onreadystatechange = x.onerror = "", cb(null, x)
    	} /* end orsc() */
    	return x.onload = x.onreadystatechange = orsc, x.onerror = cb, x.charset = "utf-8", x.src = url, document.getElementsByTagName("head")[0].appendChild(x)
    } /* end loadScript() */
    i think something like this should work, but you may have to anon pass the handler, or a pointer to it.

    Code:
    var theUrl = 'https://www.googleapis.com/customsearch/v1?key=KEY&cx=cx&q=cars&callback=hndlr';
        loadScript(theUrl, this.handlr); 
            // what to do here ???????

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles