简体   繁体   中英

How can I listen for two load events?

How can I listen for these events at the same time? (Currently not working)

<!DOCTYPE html>
<html>
  <head>
    <title>Cordova Device Ready Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    <script type="text/javascript" charset="utf-8">

        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        function onDeviceReady() {
            loadMap();
        }

        function loadMap() {
            var map, layer;

            function initialize() {
              var chicago = new google.maps.LatLng(41.850033, -87.6500523);

              map = new google.maps.Map(document.getElementById('map-canvas'), {
                center: chicago,
                zoom: 11,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              });

              layer = new google.maps.FusionTablesLayer({
                query: {
                  select: '\'Geocodable address\'',
                  from: '1mZ53Z70NsChnBMm-qEYmSDOvLXgrreLTkQUvvg'
                }
              });
              layer.setMap(map);
            }

            google.maps.event.addDomListener(window, 'load', initialize);
        }

    </script>
  </head>
  <body onload="onLoad()">
    <div id="map-canvas"></div>
  </body>
</html>

remove "onLoad" from body tag and add it like this in the script:-

window.addEventListener("load", onLoad);

This way you can listen to multiple events added on the load event.

You can look at this for better understanding

element.onload vs element.addEventListener("load",callbak,false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM