bjorn2404 / jQuery-Store-Locator-Plugin

A store locator plugin using Google Maps API version 3
MIT License
495 stars 235 forks source link

Please let the dependency for google fail gracefully.. #74

Closed lietzi closed 8 years ago

lietzi commented 9 years ago

Hi there!

the dependency for googlemaps breaks the script execution completely, if googlemaps-api fails to load or isn´t loaded intentionally.

Please add something like that at the beginning of your plugin, to fail gracefully:

if(typeof google == "undefined") {
    return;
}

My current example: I have one big js-file concatenated for all subpages, but using the storelocator only on the first page. On the subpages without googlemaps-api the javascript breaks completely with Uncaught ReferenceError: google is not defined

Thank you!

Cheers, lietzi

bjorn2404 commented 9 years ago

Hello @lietzi,

While I see your point I'm somewhat hesitant to put this in because skill level seems to vary greatly based on the questions I receive. If an error isn't displayed I wouldn't be surprised if it would confuse some people with less experience. Aren't you able to target the first page of your site with JS and only initialize the plugin if you're on it? For example:

$(function() {
    if( $('#map-container').length > 0 ) {
        $('#map-container').storeLocator();
    }
});
lietzi commented 9 years ago

Hello @bjorn2404 I already do this kind of encapsulation in my script.. Just the inclusion of your plugin breaks the whole javascript on sites, where the google maps api is not loaded at the beginning..

And this could occur when google changes their URL, or just have a maintenance issue.. or just a bad connection with timeouts.. Then the whole site is (temporarily) broken instead of just only the store locator..

Maybe something like this is more pleasant for DAUs - even if it's not elegant coding:

// Checks if google map api is loaded if(typeof google == "undefined") { console.warn('google maps api not loaded'); return; }

I do not know a "best practice" for such an issue.. :(

Cheers, lietzi

bjorn2404 commented 9 years ago

@lietzi Got it. I didn't realize that was the case and will include a fix in the next release.

bjorn2404 commented 8 years ago

Added check in v2.2.0