9

I didn't find a rightclick event in Leaflet markers so I started listening to the mousedown event. In the method that resolves this event I check

if (event.originalEvent.button == 2) {
    do_something();
    // now i want to stop propagation of the rightclick
    return;
}

This does work in that do_something() is called, but the event propagates to the map and a contextmenu is being shown. How do I prevent the event to propagate to the map?

Does this work in all major (i.e. IE) browsers?

4 Answers 4

5

My first answer was apparently not working and as I am now facing the same problem. Here is a new approach:

Leaflet supports an event called contextmenu. As found in the Leaflet event documentation here.

Fired when the user pushes the right mouse button on the map, prevents default browser context menu from showing if there are listeners on this event.

So theoretically the code

marker.on('contextmenu', function(e) {
  this.openPopup();
});

should work just fine. But apparently the implementation lags behind the documentation. As of now (Leaflet 0.4.5) the contextmenu event is apparently not yet supported on markers, although it works perfectly fine on polyline objects.

There seems to be work in the pipeline to enable this feature in the near future (see discussions here and here). So, I guess we can just hope that Leaflet works on this quickly and enables the event in the next release...

Edit: Disregard what I said about waiting for the implementation. If you download the latest development master branch and build leaflet yourself you get full support for contextmenu events on markers. Happy coding.

5

Try this internal function:

L.DomEvent.stopPropagation(event);

It should do the trick.

P.S. I never found that in any documentation but saw it used in a patch. Use at your own risk. ;)

Edit: I also found this internal function

L.DomEvent.preventDefault(event);

Which seems to be very much the same as event.preventDefault() which is in jQuery.

3
  • I used your snippet, but what happens is the same as with tmcw's solution. I rightclick my marker. It goes to the listening method. There it removes the marker (that is what rightclick should do for me) and then a context menu opens on the map where i clicked as if the whole marker thing didnt happen.
    – mrg
    Nov 26, 2012 at 22:03
  • By chance I just found this L.DomEvent.preventDefault(event);. From what I've seen (looking at the Leaflet.js source) this should be very similar to the preventDefault() provided by jQuery... Try it out! :)
    – fgysin
    Nov 27, 2012 at 8:00
  • I tried it, but the standard contextmenu i get in FF when i rightclick on an html page keeps popping up.
    – mrg
    Nov 27, 2012 at 17:55
4

Preventing default behavior in IE and all other browsers:

event.returnValue = false;
if (event.preventDefault) event.preventDefault();
2
  • It doesn't seem to work. Have you tested this in Leaflet? And is your event a leaflet event or the original DOMMouseEvent fired by the browser?
    – mrg
    Nov 22, 2012 at 9:32
  • This is for a DOM event. If Leaflet wraps events, use event.originalEvent.
    – tmcw
    Nov 23, 2012 at 3:16
2

I got a solution incidentally for stopping event.

For example, if an event is created like this - map.on("click", onMapClick);, the event can be stopped with this code - map.off("click", onMapClick);

Your Answer

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

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