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

javascript - pass variable through initialize function inside addDomListener method - Stack Overflow

programmeradmin5浏览0评论

Google maps v3 api suggest loading the initialize function using the addDomListener() method instead of attaching it to the body tag.

<script>
  function initialize() {
    // Map initialization
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
not:
<body onload="initialize()">

But, I would like to pass a variable through the initialize function: f.e. initialize(37). This only works using the body onload method, not via the addDomListener method.

Here's my question: can I do this using the addDomListener method. To put it simpler, following doesn't work, how can I make it work?

<script>
    function initialize(countryID) {
        // Map initialization
        // Do stuff with countryID
    }
   google.maps.event.addDomListener(window, 'load', initialize(37));
</script>

Google maps v3 api suggest loading the initialize function using the addDomListener() method instead of attaching it to the body tag.

<script>
  function initialize() {
    // Map initialization
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
not:
<body onload="initialize()">

But, I would like to pass a variable through the initialize function: f.e. initialize(37). This only works using the body onload method, not via the addDomListener method.

Here's my question: can I do this using the addDomListener method. To put it simpler, following doesn't work, how can I make it work?

<script>
    function initialize(countryID) {
        // Map initialization
        // Do stuff with countryID
    }
   google.maps.event.addDomListener(window, 'load', initialize(37));
</script>
Share Improve this question asked May 19, 2013 at 20:51 Peter-LPeter-L 1077 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

use a anonymous function:

google.maps.event.addDomListener(window, 'load', function(){initialize(37);});
发布评论

评论列表(0)

  1. 暂无评论