1

here is my code:

window.onload = function(){
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(function(){
    alert("Message");
  });
}

I know it will work if I put google load outside window.onload. But I want it to be inside the window.onload. Any suggestions?

Thanks

Vicheanak
  • 6,444
  • 17
  • 64
  • 98

1 Answers1

1

As far as I know, the Google loader is using document.write to add scripts to the page. document.write only works before the window has fully loaded, so it seems you can't use google.load inside of window.onload. I'd recommend putting whatever code you need inside of the google.setOnLoadCallback function - window.onload works fine in there.

Alternatively, you can autoload the Google APIs so that the onLoadCallback is unnecessary like this (see docs for more info):

<script type="text/javascript" src="http://www.google.com/jsapi?autoload={modules:[{name:gdata,version:2.x,packages:[blogger]}]}"></script>

Also check out the jQuery library because it has functions like $(document).ready() that improve greatly on window.onload.

MachineElf
  • 1,231
  • 2
  • 15
  • 28