We have an existing application that has a function that wraps the OpenLayers.Layer.Vector constructor, which gets its layer info from a KML source over http. Once this function completes, I need to run another function which will read some of the layers data from the map object.

Normally in JavaScript I would of course use a callback when executing a remote AJAX call like this. But in this case -- perhaps because I'm still pretty new to JavaScript -- I don't know how to delay the execution of my function until the Vector constructor is done. There doesn't seem to be any ability to pass a callback to the OpenLayers.Layer.Vector constructor. Any suggestions? Thanks.


How do I add the listener to the layer before the KML is loaded?

var newLay = new OpenLayers.Layer.Vector(name, {
    displayInLayerSwitcher: true,
    visibility: visible,
    styleMap: MapLayers.styleMaps.polygonMap,
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: url,
        format: new OpenLayers.Format.KML({
            maxDepth: 2,
            extractAttributes: true,
            extractStyles: useStyles

function loadEndListener() {
  alert('loading ended);

var events = new OpenLayers.Events(newLay);
events.on({"loadend": loadEndListener});
  • I've just been told that the loadend event doesn't work in all browsers, which would torpedo this approach, so, before I waste anyone's time, does anyone know if this is true? Commented Nov 10, 2011 at 16:37

2 Answers 2


You can use register event loadend on your layer. More on:


You should call this that:


I make it a long time ago, but probably that should work. On another site I found that should be tilesloaded listener, but I don't check it.

I'm sorry that so late, but I have a free.

  • Cool! So then this is what I'm trying so far (see code added to original question, I can't see how to add code to a comment) but I don't know whether it's correct and since the layer is created in the same statement that the KML layer is added, I don't know how to register the event on the layer before the KML is added. Is there a way to first construct the layer, then add the listener, then load the KML? thanks again! Commented Nov 10, 2011 at 16:08

Decided to bail on this after finding too many people having trouble with loadend working. I'm going to simply issue another server call rather than trying to figure out when my kml layer is finished loading. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.