且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的getScript加入脚本返回一个解析错误异常

更新时间:2021-12-16 07:41:53

该错误造成的,谷歌地图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');
});