Is there any ways to set google onload callback inside window.onload?
here is my code:
win开发者_运维知识库dow.onload = function(){
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
alert("Message");
});
}
Is there any way to let this code work?
Function from Google Documentation:
function loadMaps() {
google.load("maps", "2", {"callback" : mapsLoaded});
}
It seems it has callback object and you can assign here any function you like.
Google says:
You can load the Google API loader dynamically by creating a script element and setting its source to the same "https://www.google.com/jsapi?INSERT-YOUR-KEY" URL with an additional query callback parameter. The callback will be executed when the loader is ready.
I suspect the problem you are having is the use of google.load
right before the code that makes use of it. If I recall correctly, that function injects a bunch of script tags into the DOM in order to retrieve the various JavaScript dependencies. If you put the code that makes use of these dependencies in the same script tag as the load, then the dependency won't have been processed yet. Try:
<script>google.load(...);</script>
<script>
// put the rest of your code here
</script>
And see if the above works. You might also try adding some logging to see what, if anything, gets invoked as well as when the various bits of code get invoked (e.g. did you set window.onload too late?).
精彩评论