Google Map will not load in IE7 only: 'Unexpected call to method or property access'
I have a map that initializes a google map when the user clicks on a button. This fires an event that runs this method:
$self.maps = {
loadMap : function(){
var canvasDom = "m开发者_Python百科ap";
var latlng = new google.maps.LatLng(40.7608, -111.8910);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$self.vars.maps.map = new google.maps.Map(document.getElementById(canvasDom), myOptions);
},
//other methods in the object for handling other maps stuff...
};
When the last line is called to create the new map, I get the following error: "SCRIPT65535: Unexpected call to method or property access." main.js, line 31 character 347
The line of code it appears to be breaking on is:
a[Va](b);
All of this works in Firefox, Chrome, IE8 and IE9, everything I've tried except IE7 (using IE9's compatibility modes).
Update: I've set up a JSFiddle for this, initializing the map the same way, only for some reason it doesn't reproduce the IE7 error I'm getting in my application. I'm not really sure what that says about the problem.
After several painful hours of commenting out scripts looking for collisions, I started manually pasting chunks of code I was running up to the moment of creating the map. Eventually, I pared it down to the culprit.
Check this out:
//set that the map is open, and
//should remain so if the user searches again
$('form').append('<input type="hidden" name="map" value="true">');
$('#search-list-map')
.removeClass('listview')
.addClass('mapview');
//show map, hide list
$('#map').removeClass('hide');
$('#listResults').addClass('hide');
//initialize the map
$self.maps.loadMap();
loadMap() is the method I reference in the question above. Notice that input field I append to a form? It turns out that if there is an element with a name that is identical to the map div's id, and that element occurs before the map div in the DOM, it will throw an error. To my knowledge, this will only occur in IE7 (possibly IE6, I have no way to test for that).
I have reproduced the bug on JSFiddle, here. Run it under IE7 and you'll hit it.
精彩评论