最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API's initialize function without ONLOAD body tag or before end body - Stack Overflow

programmeradmin4浏览0评论

I'm working on a website that uses the DotNetNuke CMS. I've added a Google Maps map using the API, but I need to call the initialize() function when the page has loaded. Usually you'd do this with:

<body onload="initialize()">

or add the following just before the < /body> tag:

<script type="text/javascript">
    window.onload = function () {
        initialize();
    }
</script>

I however do not have access to the body tag, or exact end of the tag. Is there any other way to call this function without doing the aforementioned?

I'm working on a website that uses the DotNetNuke CMS. I've added a Google Maps map using the API, but I need to call the initialize() function when the page has loaded. Usually you'd do this with:

<body onload="initialize()">

or add the following just before the < /body> tag:

<script type="text/javascript">
    window.onload = function () {
        initialize();
    }
</script>

I however do not have access to the body tag, or exact end of the tag. Is there any other way to call this function without doing the aforementioned?

Share Improve this question asked Mar 14, 2013 at 13:56 JasonJason 4491 gold badge7 silver badges21 bronze badges 2
  • Couldn't you put that script section anywhere in the code. As long as it is towards the bottom of whatever you can edit? – Rafe Commented Mar 14, 2013 at 13:59
  • @ Rafe, I've tried, but unfortunately not possible for a specific page. It'd result into the code appearing on every single page on the website (30.000 pages instead of 1). A little too much in my opinion. I've figured out the solution though, check the answer. – Jason Commented Mar 14, 2013 at 14:04
Add a ment  | 

3 Answers 3

Reset to default 6

Not a good idea to do this on a window load event - if you've got some high-latency images or other assets loading then your google map will load very slowly. Just before the closing body tag, add this:

<script>
 initialize();
</script>

All that google needs to be ready before it builds it's map is the DOM, not every single asset on the page. The DOM is always fully loaded by the time the script loads right before the closing body tag.

EDIT

Technically, all that google maps needs to be ready before it builds it's map is the div that it is going to be placed in. So you don't even need to have the script before your closing body tag, you could have it immediately after your map div tag like this:

<html>
...
<body>
...html...

<div id="map-canvas"></div>
<script>initialize();</script>


...more html
</body>

I've figured out when I was just randomly trying something. Matter of adding the following to the headers. Long live API V3!

<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

You can call following way as well

    <div id="map-canvas"></div>

    $("#map-canvas").on("load", function () {
       initialize();            
    });
发布评论

评论列表(0)

  1. 暂无评论