Using modernizr.js / yepnope.js to load Google Maps API

1.6k Views Asked by At

Does anybody have any working code that allows one to use modernizr.load or yepnope to load the Google Maps API?

I am able to load JQuery & GMap3 using modernizr, however Google Maps API doesn't work unless I make a traditional script tag.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Please help me load Google Maps API with modernizr!</title>
    <style>
        #map { width: 500px; height: 300px; }
    </style>
    <script src="/js/modernizr-2.6.2.min.js"></script>
</head>

<body>
    <h1>Please help me load Google Maps API with modernizr!</h1>

    <div id="map">
        Placeholder
    </div>

    <script>
        function pageInit() {
            $("#map").gmap3({
                map:{
                    options: {
                        center: [18.01714, -76.750113],
                        zoom: 15,
                        mapTypeId: google.maps.MapTypeId.TERRAIN,
                    }
                }
            });
        }
    </script>


    <!-- I WANT TO REMOVE THE SCRIPT TAG BELOW AND REPLACE WITH MODERNIZR LOAD! -->
    <script src="//maps.google.com/maps/api/js?sensor=false&amp;language=en"></script>
    <!-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -->


    <script>
        Modernizr.load([{load: [
            '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',
            '/js/gmap.jquery-5.1.1.min.js',
        ],complete: function () {loaded();}},]);

        function loaded() {
            $(document).ready(function() {
                pageInit();
            });
        }
    </script>
</body>
</html>

Well logic tells me I should do the following change after removing the script tag:-

Modernizr.load([{load: [
    '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',
    '//maps.google.com/maps/api/js?sensor=false&amp;language=en',
    '/js/gmap.jquery-5.1.1.min.js',

But if I do this change the map doesn't load and I now get this error in the Javascript console:-

Load denied by X-Frame-Options: http://maps.google.com/maps/api/js?sensor=false&amp;language=en does not permit cross-origin framing.
2

There are 2 best solutions below

0
On

This is best I can come up with:-

    <script>
        Modernizr.load([{load: [
            'preload!//maps.googleapis.com/maps/api/js?sensor=false&callback=loaded',
            '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',
            '/js/gmap.jquery-5.1.1.min.js',
        ], complete: function () {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '//maps.googleapis.com/maps/api/js?sensor=false&callback=loaded';
            document.body.appendChild(script);
        }}]);

        function loaded() {
            $(document).ready(function() {
                pageInit();
            });
        }
    </script>    

It seems quicker with the preload! However I think it's grabbing the file twice judging by what Firebug tells me.

0
On

Altering the Access-Control-Allow-Origin head to allow Google's API URL to your page header will get rid of the security issue.

For example Apache .htaccess:

Header set Access-Control-Allow-Origin "http://maps.googleapis.com/maps/api/js*"

or via php:

<?php header('Access-Control-Allow-Origin: http://maps.googleapis.com/maps/api/js*', true);

Although I am not 100% on the security ramifications of doing such a thing. Google API is a big library. See http://code.google.com/p/html5security/wiki/CrossOriginRequestSecurity and the section Processing rogue COR

Side note, I'm not a fan of this also because it adds the additional header to every request.