[OpenLayers-Dev] Vector layer fails, if constructed before the page has finished loading

Jeff Dege jdege at korterra.com
Mon Mar 19 16:38:45 EDT 2007


OK - further info.

You can't declare a vector layer in inline javascript, you have to
declare it in a function that's called by a client-side event.

The vectorfeatures example declares a vector layer in an init()
function, and init() is called on the onload event of <body>.  This
worked.

My ASPX conversion called init() in an emitted Javascript block, placed
near the end of the file
by ClientScript.RegisterStartupScript().  This did not work.

What works:

   <html>
   <head runat="server">
      <title>Untitled Page</title>
      <script type="text/javascript"
src="openlayers/lib/OpenLayers.js"></script>
      <script type="text/javascript">
         <!--
         var map;
         function init(){
            map = new OpenLayers.Map( $('map') );
            var vectorLayer = new OpenLayers.Layer.Vector("Simple
Geometry");
		[...]
         }
         // -->
      </script>
   </head>
   <body onload="init();">
      <form id="form1" runat="server">
      <div id="map"></div>
      </form>
   </body>
   </html>

What doesn't work:

   <html>
   <head runat="server">
      <title>Untitled Page</title>
      <script type="text/javascript"
src="openlayers/lib/OpenLayers.js"></script>
      <script type="text/javascript">
         <!--
         var map;
         function init(){
            map = new OpenLayers.Map( $('map') );
            var vectorLayer = new OpenLayers.Layer.Vector("Simple
Geometry");
		[...]
         }
         // -->
      </script>
   </head>
   <body>
      <form id="form1" runat="server">
      <div id="map"></div>
         <script type="text/javascript" defer="defer">
         init();
         </script>
      </form>
   </body>
   </html>

The vector layer constructor fails, if it's called before the page has
entirely loaded.

None of the other layers I've worked with have this dependency.



More information about the Dev mailing list