开发者

Google AJAX API Loader

开发者 https://www.devze.com 2022-12-20 07:03 出处:网络
I am having a problem loading the Google AJAX开发者_运维知识库 APIs in response to user input instead of when the page loads.

I am having a problem loading the Google AJAX开发者_运维知识库 APIs in response to user input instead of when the page loads.

This works:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("search", "1");
    google.setOnLoadCallback(function() { alert("loaded"); });
</script>

But this doesn't:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
    function clicked() {
        google.load("search", "1");
        google.setOnLoadCallback(function() { alert("loaded"); });
    }
</script>

The clicked function is a handler for a simple link.

Does anyone know why?


Based on the IE Response, it may be the case that the Google AJAX APIs haven't loaded by the time you click the button, therefore the "google" object is undefined.

Try this (http://code.google.com/apis/ajax/documentation/):

function mapsLoaded() {
  var map = new google.maps.Map2(document.getElementById("map"));
  map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
}

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

function initLoader() {
  var script = document.createElement("script");
  script.src = "http://www.google.com/jsapi?key=ABCDEFG&callback=loadMaps";
  script.type = "text/javascript";
  document.getElementsByTagName("head")[0].appendChild(script);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消