I use the following HTML tag to load the Google Maps API :
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
However, it is blocking the loading everything that's below it in the HTML until the script is loaded by the browser.
Is there a way to make this loading non-blocking?
Make sure WordPress, your plugins and the theme on your website are up to date. Check which WordPress plugins and themes show the maps from Google Maps. Check their settings to make sure you can now paste your Google Maps API key.
This code gives you a defer function that takes an url and an optional callback. It asynchronously loads your script without blocking page rendering. I've put in a protection so it won't load the same scritp twice, so you can naively call it as many times as you like.
defer = (function () {
var urls = [];
return function (url, callback) {
var inc;
if (url && urls.indexOf(url) === -1) {
inc = document.createElement('script');
inc.async = true;
inc.src = url;
inc.onload = callback || function () {};
document.getElementsByTagName('head')[0].appendChild(inc);
}
}
}());
defer('http://maps.google.com/maps/api/js?sensor=false');
This works for any external javascript that doesn't fail on async loading.
Yes, you can load it asynchronously. Check out this part of the docs: http://code.google.com/apis/maps/documentation/javascript/basics.html#Async
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With