My website loads the google maps api when I click on the location tab, then initializes the map. I don't want to load the api before this tab is clicked. I noticed it loaded the api every time I clicked the tab (causing memory usage for the site to increase). The if/else
statement I wrapped it in doesn't work in a click()
function. I'm also loading the api and my mapconstructor.js asynchronously as per https://stackoverflow.com/a/11803418/4184379. I've checked the similar answers on if/else
not working in a click, but they haven't helped.
$("[id=location-tab]").click(function() {
if (typeof google.maps.event.trigger == 'undefined') {
$.when(
$.getScript("https://maps.googleapis.com/maps/api/jsv=3.exp&sensor=false&callback=initialize" ),
$.getScript( "assets/js/gmap.js" ),
$.Deferred(function( deferred ) {
$( deferred.resolve );
})
).done(function() {
initialize();
});
}
else {
google.maps.event.trigger(map_canvas, 'resize');
}
});