Google Maps API V3 窗口加载事件监听器在某些浏览器中不起作用

发布于 2024-11-29 12:50:16 字数 1737 浏览 1 评论 0原文

一个奇怪的行为: 关于地图内事件的文档的最后部分API 表示

>  If you've been reading the documentation this far, you're probably
> already familiar with one DOM event: the window.onload event, which
> we've handled within the <body> tag. We use this event to trigger the
> initial Javascript code once an HTML page is fully loaded, as shown
> below: <script>   function initialize() {
> 
>     // Map initialization
> 
>   } </script> <body onload="initialize()">   <div
> id="map_canvas"></div> </body> Although this event is attached to the
> <body> element here, this event is really a window event indicating
> that the DOM hierarchy below the window element has been fully built
> out and rendered. Although easy to understand, having an onload event
> within a <body> tag mixes content with behavior. Generally, it is good
> practice to separate your content code (HTML) from your behavioral
> code (Javascript) and provide your presentation code (CSS) separately
> as well. You can do so by replacing the inline onload event handler
> with a DOM listener within your Maps API Javascript code like so:
> <script>   function initialize() {
> 
>     // Map initialization
> 
>   }
>      google.maps.event.addDomListener(window, 'load', initialize);
> </script> <body>   <div id="map_canvas"></div> </body>

google.maps.event.addDomListener(window, 'load',initialize); 不适用于 Firefox/Safari,但 确实... 有什么想法吗?

A strange behaviour:
The last part of the Documentation on events inside maps API says that

>  If you've been reading the documentation this far, you're probably
> already familiar with one DOM event: the window.onload event, which
> we've handled within the <body> tag. We use this event to trigger the
> initial Javascript code once an HTML page is fully loaded, as shown
> below: <script>   function initialize() {
> 
>     // Map initialization
> 
>   } </script> <body onload="initialize()">   <div
> id="map_canvas"></div> </body> Although this event is attached to the
> <body> element here, this event is really a window event indicating
> that the DOM hierarchy below the window element has been fully built
> out and rendered. Although easy to understand, having an onload event
> within a <body> tag mixes content with behavior. Generally, it is good
> practice to separate your content code (HTML) from your behavioral
> code (Javascript) and provide your presentation code (CSS) separately
> as well. You can do so by replacing the inline onload event handler
> with a DOM listener within your Maps API Javascript code like so:
> <script>   function initialize() {
> 
>     // Map initialization
> 
>   }
>      google.maps.event.addDomListener(window, 'load', initialize);
> </script> <body>   <div id="map_canvas"></div> </body>

Somehow google.maps.event.addDomListener(window, 'load', initialize); doesn't work for Firefox/Safari, but <body onload="initialize()"> does...
Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文