www.webdeveloper.com
Results 1 to 3 of 3

Thread: google api loading doesn't work

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    google api loading doesn't work

    Hi,

    Can anyone find why this Javascript doesn't work?

    Code:
    window.onload = function(){
      //create script element
      var scriptElem  = document.createElement("script");
      scriptElem.type = "text/javascript";
      scriptElem.src = "http://www.google.com/jsapi";
    
      //include the script into the header
      document.getElementsByTagName("head")[0].appendChild(scriptElem);
    
      //load google search API
      google.load('search', 1); //"google" is undefined!!!
    }
    I'm developing a Google Chrome Extension.
    I thought the google api was not included properly, but "google.load('search', 1)" is valid on Firebug (a chrome debugger).

    Thanks.

  2. #2
    Join Date
    Nov 2011
    Posts
    3
    I solved this problem myself.

    As Chrome Extension states, ContentScripts cannot use variables or functions defined by webpages, or by other content script. So you cannot use "google" defined by the webpage.

    Instead, you can use Background.html to do the same thing.

    Code:
    //background.html
    <html>
    <head>
    <script type="text/javascript" src="https://www.google.com/jsapi?key=xxxx"></script>
    <script type="text/javascript" src="myscript.js"></script>
    </head>
    </html>
    Code:
    //myscript.js
    google.load('search', 1); //NO ERROR
    Cheers

  3. #3
    Join Date
    Nov 2011
    Posts
    3
    Hi,

    (Next problem following the issue)
    While "google" is now available variable as show in the frist reply, you should find another problem.

    Code:
    .setSearchCompleteCallback(this, callback, arg)
    throws "Uncaught TypeError: Cannot call method 'appendChild' of null"(exception).

    Can anyone know how to solve this?

    Thanks..

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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