这是我的难题:
我有一个使用 Google Maps V3 和 jQuery 的页面。它在 FF5、Chrome 和 Safari 本地运行良好。
上传到网站后,我在尝试使用 google 对象的第一行收到“google 未定义”错误。
var defaultLocation = new google.maps.LatLng(lat, lng);
它仅发生在 FF 且仅发生在远程< /strong> (即,如果我将文件加载到本地 FF 中,效果很好)。不管怎样,Chrome 和 Safari 似乎都运行良好,我的 Android 和 iPod 浏览器也是如此。
这是我到目前为止所尝试的:
- 移动
到
部分的顶部。
- 将
$(function() {...});
的所有内容移至名为 initialize()
的函数,并添加
- 使用脚本和 css 文件顺序播放
- 粘贴了 URL http://maps.google.com/maps/api/js?sensor=false 转换为 FF 地址框并验证我得到了合法的脚本
,但由于这仅发生在远程计算机上的 FF 中并且在其他情况下运行良好,我认为它与我的代码没有任何关系。也许FF5中的加载顺序是搞砸的。也许它对网络资源的优先级划分与其他浏览器不同。我现在真的不知道该怎么办。
如有任何帮助,我们将不胜感激。
盖伊
更新:
只是想添加以下事实:在 Mac 上尝试了前一个之后,我在 Windows 中尝试了 FF5,并且复制了完全相同的行为。
为了更好地衡量,我也尝试了 Pale Moon - 相同的结果。 Chrome 14、Opera 11.50 甚至该死的 IE9(未包含在测试计划中)都可以工作。只是现在在 Mac 和 Windows 7 上都运行的 FF5 在该页面上失败了。
Here's my conundrum:
I have a page that uses Google Maps V3 and jQuery. It all worked well locally in FF5, Chrome and Safari.
Once I uploaded to a web site, I get a "google is not defined" error on the first line that I try to use a google object
var defaultLocation = new google.maps.LatLng(lat, lng);
It only occurs in FF and only occurs remotely (i.e., if I load the file into FF locally, it works well). Chrome and Safari seem to be working great regardless, as is my Android and iPod browsers.
Here's what I tried so far:
- Moved
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
to top of the <head>
section.
- Moved all content of
$(function() {...});
to a function called initialize()
and added <body onload="initialize()">
- Played with scripts and css files order
- Pasted the URL http://maps.google.com/maps/api/js?sensor=false into FF address box and verified I'm getting the legit script
But since this is only happening in FF on a remote machine and works well otherwise, I don't think it has anything to do with my code. Maybe the load order in FF5 is screwed. Maybe it prioritizes network resources differently than other browsers. I really do not know what to make of it at this point.
Any help is appreciated.
Guy
Update:
Just wanted to add the following fact: After trying the previous on a Mac, I tried FF5 in Windows, and have replicated the exact same behavior.
For good measure, I tried Pale Moon as well - same results. Chrome 14, Opera 11.50 and even frickin' IE9 (which wasn't included in the test plan) work. It just FF5, now on both Mac and Windows 7, that fails on that page.
发布评论
评论(15)
尝试使用这个:
Try using this:
试试这个:
它对我有用......
重点是,将 HTTP 更改为 HTTPS
try this:
it works for me...
the point is, change HTTP to HTTPS
添加脚本的类型
所以重要的部分是类型text/javascript。
Add the type for the script
So the important part is the type text/javascript.
而不是
使用
这个,这对我有用。
instead of this
use this
and this worked for me.
从 Firefox 23 开始,默认设置启用混合内容阻止(本地禁用)。如果您使用安全连接和一些不安全的 API,它也会阻止来自 Google 的一些 API。
要禁用它,当存在一些不安全内容时,您必须单击位置栏中出现的盾牌,设置“禁用保护”,然后您必须查看位置栏中的黄色感叹号:(
https://blog.mozilla.org/.../mixed-content-blocking-enabled-in-firefox-23/
您始终可以尝试将 http 协议替换为 https API url。如果 API 也在安全连接中提供 - 您将不会看到任何警告
。
From Firefox 23, there is Mixed Content Blocking Enabled set by default (locally disabled). It blocks some APIs from Google also if you use secure connection and some unsecure APIs.
To disable it you'll have to click shield which appears in location bar when there are some unsecure contents, set 'Disable protection' and then you'll have to look at yellow exclamation mark in location bar :(
https://blog.mozilla.org/.../mixed-content-blocking-enabled-in-firefox-23/
You can always try also replace http protocol with https in the API url. If API is provided also in secure connection - you will not see any warnings.
It works for me.
您可以尝试以下操作:
首先,添加
async defer
。这指定脚本一旦可用且页面完成解析后将异步执行。其次,将
initMap()
函数添加为 html 内脚本标记中的回调。这样,地图将在 document.ready 和 window.onload 之前初始化:最后,您可以在 js 文件中使用地图对象。
You can try the following:
First, add
async defer
. This specifies that the script will be executed asynchronously as soon as it is available and when the page has finished parsing.Second, add the
initMap()
function as a callback in a script tag inside your html. In this way the map will be initialized before the document.ready and window.onload:Finally, you can use the map object inside your js files.
更改
并使其
有效:)
Changed the
and made it
It worked :)
我认为最简单的技巧是:
当您的应用程序准备就绪时,它将初始化地图。
检查一下。
I think the easiest trick is:
It will init the map when your app is ready.
Check this.
请检查您调用的顺序、您的库、以下顺序
<脚本类型 = "text/javascript" src = "../resources/googleMaps /jquery-ui.min.js">
<脚本类型 = "text/javascript" src="http:// maps.googleapis.com/maps/api/
METOD
<脚本类型 = "text/javascript" src = "googleMaps/mapa.js">
我遇到了这个问题,我刚刚调整了我的订单。
Please check the order you are calling, your libraries, the following order
<script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.js"></script>
<script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.min.js"></script>
<script type = "text/javascript" src="http://maps.googleapis.com/maps/api/
METOD
<script type = "text/javascript" src = "googleMaps/mapa.js"></script>
I was with this problem, I just adjusted my order.
就我而言,我通过 http 加载 google 脚本,而我的服务器有 SSL 并且通过 https 加载。所以我必须通过 https 加载脚本
In my case I was loading the google script via http while my server had SSL and its being loaded over https. So I had to load script via https
我不确定,但这是我最好的建议。
您正在使用 jQuery。因此,您实际上应该使用 $(function() {... }); 而不是设置事件进行初始化。使用此选项的原因是,它可以确保页面上的所有脚本都已加载,并且您不会像使用 onload body 标记那样仅限于一个 init 函数。
另外,请确保您的 Javascript 代码位于 Google 包含之后。否则,您的代码可能会在创建 Google 对象之前执行。
我还建议您查看一下有关事件顺序的页面。
http://dean.edwards.name/weblog/2005/09/busted/
I don't know for sure but here are my best suggestions.
You're using jQuery. So instead of setting the event you should really be using $(function() {... }); to do your initialization. The reason to use this is that it ensures that all the scripts on the page have loaded and you're not limited to just one init function like you are with the onload body tag.
Also, be sure your Javascript code is after the Google include. Otherwise your code might execute before the Google objects are created.
I would also suggest taking a look at this page about event order.
http://dean.edwards.name/weblog/2005/09/busted/
如果您在 API 调用中提到了回调函数,则应该将您的 google 地图代码包装在具有相同名称的 JavaScript 函数中。否则你会得到这个错误。
If you have mentioned a call back function in API call, you should wrap your google map code inside a JavaScript function with same name. Else you will get this error.
我多次遇到“谷歌未定义”。
可能 Google Script 有一些问题,无法用 FF-addon 很好地加载
顺便提一句。
FF 有重新启动选项(如窗口重新启动)
帮助>禁用附加组件后重新启动
I faced 'google is not defined' several time.
Probably Google Script has some problem not to be loaded well with FF-addon
BTW.
FF has restart option ( like window reboot )
Help > restart with Add-ons Disabled
我在使用 Gmap3 时遇到了同样的错误“google 未定义”。问题是我在包含“google”之前包含了“gmap3”,所以我颠倒了顺序:
I had the same error "google is not defined" while using Gmap3. The problem was that I was including 'gmap3' before including 'google', so I reversed the order:
另一个对我有帮助的建议:
这是发生在我身上的事情=>我加载页面时,我的脚本每 3 次运行一次,错误是“google 未定义”。
我使用谷歌地图的函数是在我的 jQuery 文档的就绪函数中,
我只是添加了这段代码以确保它有效:
它就像一个魅力。如果您想了解有关文档就绪和窗口加载之间差异的更多详细信息,这里有一篇很棒的文章:
window.onload 与 $(document).ready()
Another suggestion that helped me:
Here is what happent to me => My script was working once in 3 time I was loading the page and the error was the «google is not defined».
My function using the google map was in my jQuery document's ready function
I simply added this code to make sure it works:
It works like a charm. If you want more details on difference between document ready and window load, here is a great post about it:
window.onload vs $(document).ready()