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
1 Answer
Reset to default 10use a anonymous function:
google.maps.event.addDomListener(window, 'load', function(){initialize(37);});