views:

8

answers:

0

Hi. Following code:

  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <script type="text/javascript">
        function loadGoogle() {
            google.load("jquery", "1");
            function OnLoad(){
                alert("Loaded!");
            }
            google.setOnLoadCallback(OnLoad);
        }
        loadGoogle();
    </script>
  </head>
  <body onload="">
  </body>

performs as it was supposed to: outputs "Loaded!". But if I move "loadGoogle()" to body onload event it won't work:

<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <script type="text/javascript">
        function loadGoogle() {
            google.load("jquery", "1");
            function OnLoad(){
                alert("Loaded!");
            }
            google.setOnLoadCallback(OnLoad);
        }
    </script>
  </head>
  <body onload="loadGoogle();">
  </body>
</html>

That's important because if I try to load Google API dynamically (not from HTML but from JS code) it fails. How can I do that AFTER loading a page and why the second sample won't work?