Javascript IE8 中的 Google Maps API 加载失败(“'google' 未定义”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6335019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps API load failure ("'google' is undefined") in IE8
提问by Trott
When I load http://maps.google.com/maps/api/js?sensor=falsein a script tag, everything works fine for me in Chrome, Safari, Firefox, and IE9.
当我在脚本标签中加载http://maps.google.com/maps/api/js?sensor=false时,我在 Chrome、Safari、Firefox 和 IE9 中一切正常。
However, when I look in IE9 in compatibility mode(or, I'm told, in IE8) the map does not load and "'google' is undefined" is logged in the console.
但是,当我在兼容模式下查看 IE9时(或者,我被告知,在IE8 中)地图不会加载并且“'google'未定义”被记录在控制台中。
Here's the relevant code, with the line that is triggering the error identified with a comment:
这是相关代码,其中触发错误的行用注释标识:
<html>
<head>
<title>Test Map</title>
<script type="application/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
<div id="map_canvas"></div>
<script type="text/javascript">
var lat=37.763154;
var lon=-122.457941;
var initialZoom=17;
var mapTypeId = 'Custom Map';
var mapStyle = [{featureType:"landscape", elementType:"all", stylers:[{hue:"#dae6c3"},{saturation:16},{lightness:-7}]},
{featureType:"road", elementType:"geometry", stylers:[{hue:"#ffffff"},{saturation:-100},{lightness:100}]}];
//**The error is tripped in this next line, again only in IE9 compatibility mode or IE8*
var styledMap = new google.maps.StyledMapType(mapStyle);
var mapType = new google.maps.ImageMapType({
tileSize: new google.maps.Size(256,256),
getTileUrl: function(coord,zoom) {
return "img/tiles/"+zoom+"/"+coord.x+"/"+coord.y+".png";
}
});
var map = new google.maps.Map(document.getElementById("map_canvas"),
{center:new google.maps.LatLng(lat,lon),
mapTypeId:google.maps.MapTypeId.ROADMAP,
zoom:initialZoom,
mapTypeControl:false});
map.overlayMapTypes.insertAt(0, mapType);
map.mapTypes.set(mapTypeId, styledMap);
map.setMapTypeId(mapTypeId);
</script>
</body>
</html>
So, for some reason, and only in IE9+compatibility-mode and IE8, the script tag specifying http://maps.google.com/maps/api/js?sensor=falseisn't loading and/or executing before the subsequent embedded script in the body.
因此,出于某种原因,并且仅在 IE9+compatibility-mode 和 IE8 中,指定http://maps.google.com/maps/api/js?sensor=false的脚本标记不会在随后在正文中嵌入脚本。
Are others able to replicate? How do I correct this problem?
其他人能复制吗?我该如何解决这个问题?
采纳答案by Trott
The problem, apparently, is that IE 8 doesn't grok "application/javascript". I changed it to "text/javascript" in the <script>
tag in the <head>
section and now my code works. And, of course, if I change it back to "application/javascript", then it stops working.
问题显然是 IE 8 不理解“应用程序/javascript”。我<script>
在该<head>
部分的标签中将其更改为“text/javascript” ,现在我的代码可以工作了。当然,如果我把它改回“application/javascript”,它就会停止工作。
回答by subsci
My google maps v3 site stopped working in IE 9 compatibility view mode, IE 8 and IE7.
我的谷歌地图 v3 站点在 IE 9 兼容视图模式、IE 8 和 IE7 中停止工作。
Reason: Error in java-script using jQuery only caught when using the IE F12 Developer tools to examine the script. Here is the offending line. The error was missing single quotes from the token, class.
原因:仅在使用 IE F12 开发人员工具检查脚本时才发现使用 jQuery 的 java-script 中的错误。这是违规行。错误是缺少令牌类中的单引号。
$('<tr>', { 'class': country }).appendTo(tableSelector).append(h1).append(h2);
At first I pursued a false lead, thinking it was the addition of a key= http://maps.googleapis.com/maps/api/js?key=MY_KEY_FROM_API_CONSOLE&sensor=false
一开始追了个假的,以为是加了key= http://maps.googleapis.com/maps/api/js?key=MY_KEY_FROM_API_CONSOLE&sensor=false
The lesson is: use tools, Firebug or IE tools or whatever, to examine your javascript for introduced issues.
教训是:使用工具、Firebug 或 IE 工具或其他工具,检查您的 javascript 是否存在引入的问题。
回答by Guy R
One guess is that you're page works over https while the request from Google is http. convert the Google request to https and the error will disappear. this worked for me.
一种猜测是您的页面通过 https 工作,而来自 Google 的请求是 http。将谷歌请求转换为https,错误就会消失。这对我有用。
See : Possible solution
请参阅:可能的解决方案
回答by josh.trow
IE is downloading then attempting to execute the JS on your local machine, while the other browsers are simply opening it as a text file. You can find the downloaded JS from IE in wherever stuff downloads to by default.
IE 正在下载然后尝试在本地机器上执行 JS,而其他浏览器只是将其作为文本文件打开。默认情况下,您可以在任何东西下载到的任何地方找到从 IE 下载的 JS。
EDIT: In light of updates, see this Fiddle to see a sort-of working fix. http://jsfiddle.net/h6rc3/
编辑:根据更新,请参阅此 Fiddle 以查看某种工作修复。http://jsfiddle.net/h6rc3/
回答by brenjt
Make sure that IE isn't in offline mode. Sounds like the browser isn't connecting to the internet.
确保 IE 未处于离线模式。听起来浏览器没有连接到互联网。