简体   繁体   English

Google AJAX API加载程序

[英]Google AJAX API Loader

I am having a problem loading the Google AJAX APIs in response to user input instead of when the page loads. 我在响应用户输入而不是页面加载时加载Google AJAX API时遇到问题。

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. clicked函数是一个简单链接的处理程序。

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. 根据IE响应,可能是您单击按钮时尚未加载Google AJAX API的情况,因此未定义“ google”对象。

Try this ( http://code.google.com/apis/ajax/documentation/ ): 试试这个( 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM