jQuery getScript返回一个解析错误exception
我正在尝试使用获取Google Map脚本的$.getScript
函数加载两个脚本,然后在加载之后,我得到另一个脚本( goMap
),它可以轻松制作地图小程序。
但是,在加载时,第一个获取Google Map API的脚本是好的,然后第二个脚本返回一个解析错误并显示:
TypeError:’undefined’不是构造函数’
然而,我不知道它在哪里引用或哪一行,我认为它必须尝试在这个文件上执行Geocoder(第一行(function($){
:
http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js
这是我的代码:
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() { $.getScript('../js/gomap.js').done(function() { // this never gets called alert('gomap loaded'); }).fail(function(jqxhr, settings, exception) { alert(exception); // this gets shown }); }).fail(function() { alert('failed to load google maps'); });
我尝试更改AJAX设置以将async
设置为false
,但它根本没有帮助。
该错误是由于Google Maps API希望使用加载到头脑中
。
如果由于某种原因你不能这样做,那仍然是希望。 Google Maps API不起作用,因为它使用document.write
在页面中注入依赖项。 要使代码生效,您可以覆盖本机document.write
方法。
演示: http : //jsfiddle.net/ZmtMr/
var doc_write = document.write; // Remember original method; document.write = function(s) {$(s).appendTo('body')}; $.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() { $.getScript('../js/gomap.js').done(function() { alert('gomap loaded'); document.write = doc_write; // Restore method }).fail(function(jqxhr, settings, exception) { alert(exception); // this gets shown document.write = doc_write; // Restore method }); }).fail(function() { alert('failed to load google maps'); });
@lolwut试试
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() { alert(11); $.getScript('http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js').done(function() { // this never gets called alert('gomap loaded'); }).fail(function(jqxhr, settings, exception) { alert(exception); // this gets shown }); }).fail(function() { alert('failed to load google maps'); });
如果这有效,那么你是相对路径../js/gomap.js是错误的!