my javascript code
var script = document.createelement('script'); script.src = 'https://maps.googleapis.com/maps/api/js?libraries=places'; script.type = 'text/javascript'; document.getelementsbytagname('head')[0].appendchild(script); script.onload = function(){ var autocomplete; autocomplete = new google.maps.places.autocomplete( (document.getelementbyid('autocomplete')), { types: ['geocode'] }); i error
cannot read property 'autocomplete' of undefined @ htmlscriptelement.script.onload (mabapamavo.js:18:40) the above same code works when load library using script tag. want load library dynamically using javascript.
what mistake making?
the error message telling google.maps.places undefined. instead of using script.onload, should using initialization callback, outlined in documentation.
here's code should like:
var script = document.createelement('script'); script.src = 'https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize'; script.type = 'text/javascript'; document.getelementsbytagname('head')[0].appendchild(script); initialize = function() { var autocomplete = new google.maps.places.autocomplete( (document.getelementbyid('autocomplete')), { types: ['geocode'] });
Comments
Post a Comment