jQuery的getScript加入脚本返回一个解析错误异常 [英] jQuery getScript returns an parse error exception

查看:428
本文介绍了jQuery的getScript加入脚本返回一个解析错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载两个脚本与得到谷歌地图脚本的 $。getScript加入函数,然后就是装后,我得到另一个脚本(的 goMap ),这使得地图小程序容易进行

I'm trying to load two scripts with the $.getScript function of getting Google Map script, then after that is loaded, I get another script (goMap) which makes map applets easily to be made.

不过,加载时,让谷歌地图API的第一个剧本是好的,那么第二个脚本返回一个语法错误,并且显示了这一点:

However, when loaded, the first script of getting Google Map API is good, then the second script returns a parse error and shows this:

类型错误:未定义不是构造函数

TypeError: 'undefined' is not a constructor'

不过,我不知道是从或行引用,我认为它必须尝试执行的地理codeR此文件(第一行(功能之后($ ){

Yet, I don't know where that is referencing from or which line, I think it must be trying to execute the Geocoder on this file (first line after (function($){:

http://www.pittss.lv/ jQuery的/ gomap / JS / jquery.gomap-1.3.2.js

下面是我的code:

$.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设置来设置异步,但它并没有帮助的。

I tried changing the AJAX settings to set async to false, but it didn't help at all.

推荐答案

该错误造成的,谷歌地图API预计的头部被加载,用事实<脚本SRC = http://maps.google.com/maps/api/js?sensor=true>< / SCRIPT>

The error is caused by the fact that the Google Maps API expects to be loaded in the head, using <script src="http://maps.google.com/maps/api/js?sensor=true"></script>.

如果您不能这样做,因为某些原因,还是有希望。在谷歌地图API是不行的,因为它使用文件撰写注入在页面的相关性。为了获得code的工作,可以覆盖本机文件撰写方法。

If you cannot do that for some reason, there's still hope. The Google Maps API does not work, because it uses document.write to inject a dependency in the page. To get the code to work, you can overwrite the native document.write method.

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');
});

这篇关于jQuery的getScript加入脚本返回一个解析错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆