English 中文(简体)
在 IE8 中造成 Google 地图错误的原因是什么?
原标题:What is causing a Google Maps error in IE8?

我们正在使用谷歌地图,并确定了一个似乎只在IE8中发生的问题(甚至可能在下文中发生 ) 。 功能在FF、Chrome、IE9中运作正确。

错误发生的代码是:

google.load("maps", "3.x", { other_params: "sensor=false" });
    var mapdiv = null;
    $(function () {
        mapdiv = document.getElementById("map");
        map = new google.maps.Map( mapdiv, {
            zoom: 1,
            center: new google.maps.LatLng(6, 35),
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        });
        var latlngbounds = new google.maps.LatLngBounds( );

特别是在这一行:

map = new google.maps.Map( mapdiv, {
    zoom: 1,
    center: new google.maps.LatLng(6, 35),
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
});

错误是:

对象不支持此属性或方法

我对 IE dev 工具玩过一点游戏,如果我用 < code> map {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}} 替换 map {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}{{{{{{{{{{{{{}}}}}}}}}}{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}},但我不知道 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

有人知道这里发生了什么吗?

最佳回答

当行:

map = new google.maps.Map(mapdiv, { zoom: 1, center: new google.maps.LatLng(6, 35), disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.TERRAIN });

执行后 JavaScript 解释器将开始跨过范围链, 查找 < code> map 被宣布的位置。 由于它不是本地宣布的, 即 < code> var map =... , 它无法在本地范围找到它, 最终被添加到全球范围 。

在您的情况下, map 已被全球范围定义为 window.map , 因为它是您页面上 div 的代号。 在您看不到错误消息的所有浏览器中, window.map 被设置为一个新的 gogogle.maps.maps.Map 对象。 出于某种原因, 无论好坏, IE8 都会允许您创建一个全球变量, 其名称与现有的全球变量冲突, 指 DOM 元素 。

你可以用几种不同的方式解决这个问题:

  1. Create a local var map
  2. Change the id of your div from map to something else
  3. 如果您需要将 map 存在全球范围, 请使用 window.map=... 设定为

    (3b) 将变量名称更改为与 window.map 不冲突的名称,例如 myMap=新的google.maps.maps.map (... )

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.