Fix unspecified error with IE7 and IE8 with Google Maps v3Fix unspecified error with IE7 and IE8 with Google Maps v3

Posted November 27th, 2010 in Javascript

I upgraded Google Maps on my New Zealand Running Calendar website yesterday from the API version 2 to version 3 and started getting "unspecified errors" with the Javascript in Internet Explorer versions 7 and 8.

Load Google Maps onload

IE7's error console is pretty hopeless at telling you where the error occured but fortunately the error also happened in IE8 and it was able to tell me the error was occuring in the Google Maps API Javascript file.

However the error isn't on Google's part: the map rendering needs to be done after the page has completed loading.

My code originally looked like this:

<div id="googleMap"></div>
<script type="text/javascript">
    initGoogleMap( ... );
</script>

initGoogleMap is my own Javascript function and it's called from the page template so stuff from the database can be passed to the function to center and zoom it, add map markers and routes etc.

Putting the function into a jQuery $(document).ready block doesn't work either. Instead you need to put it into a window.onload block like so:

<script type="text/javascript">
    window.onload = function() {
        initGoogleMap( ... );
    };
</script>

Or alternatively in the <body> tag:

<body onload="initGoogleMap( ... )">

I haven't tested the body onload version myself and used the window.onload method. Once this was done the issue was solved.

Related posts:

Comments

blog comments powered by Disqus