JQuery getScript returns parsing error exception

I try to load two scripts using the $.getScript function to get the Google Map script, after which I get another script ( goMap ), which makes it easy to create map applets.

However, when loading, the first script to get the Google Maps API is good, then the second script returns a parsing error and shows this:

TypeError: 'undefined' is not a constructor

However, I don’t know where is the link or which line, I think that it should try to execute Geocoder in this file (the first line after (function($){ :

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

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

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

+4
source share
2 answers

The error is because the Google Maps API expects it to be loaded into the head using <script src="http://maps.google.com/maps/api/js?sensor=true"></script> .

If you cannot do this for some reason, there is still hope. The Google Maps API does not work because it uses document.write to enter the dependency on the page. To make the code work, you can overwrite your own document.write method.

Demo: 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'); }); 
+4
source

@lolwut try

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

If this works, then you relative path ../js/gomap.js is wrong!

0
source

All Articles