title: How to use Google API in a js file?
body: <p>I want to use Google API inside a js file and how can I use it? I tried to use google.load() directly in the js file but was told that google is not defined. Then I tried to use the following code</p>
  
  <pre><code>var s = document.createElement('script');
  </code></pre>
  
  <p>but was told that document is not defined.
  What should I do to use google api inside a js file? Thank you.</p>
  
  <p>Here what I want to use is the <a href="https://developers.google.com/feed/" rel="nofollow">Google Feed API</a>.</p>
  
  <p>The html code I used is</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;
              Example
          &lt;/title&gt;
          &lt;script src="https://www.google.com/jsapi" type="text/javascript"&gt;&lt;/script&gt;
      &lt;/head&gt; 
  
      &lt;body&gt;
          &lt;div&gt;
              &lt;p id="computation_results"&gt;please wait, computing … &lt;/p&gt;
          &lt;/div&gt;
          &lt;script&gt;
              var worker = new Worker('numberworker.js');
              worker.postMessage({first:123,second:456});
              worker.onmessage = function (event)
              {
                  alert(event.data);
                  document.getElementById('computation_results').textContent = event.data;
              };
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the js file where I want to use the api is</p>
  
  <pre><code>// Our callback function, for when a feed is loaded.
  function feedLoaded(result) {
  if (!result.error) {
      var container = document.getElementById("content");
      container.innerHTML = '';
      for (var i = 0; i &lt; result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(entry.title));
        container.appendChild(div);
      }
    }
  }
  function OnLoad() {
    var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
    feed.load(feedLoaded);
  }
  
  onmessage = function (event) 
  { 
      var fileref=document.createElement('script');
      var filename="https://www.google.com/jsapi";
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", filename);
      document.getElementsByTagName("head")[0].appendChild(fileref);
  //  google.load("feeds", "1");
  //  google.setOnLoadCallback(OnLoad);
      var first=event.data.first; 
      var second=event.data.second; 
      postMessage("Work done! "+ " "+first+" "+second);
  }; 
  </code></pre>