Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass variable through initialize function inside addDomListener method

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>
like image 490
Peter-L Avatar asked May 19 '13 20:05

Peter-L


1 Answers

use a anonymous function:

google.maps.event.addDomListener(window, 'load', function(){initialize(37);});
like image 58
Dr.Molle Avatar answered Oct 30 '22 14:10

Dr.Molle