Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

My first answer was apprently not working and as I am now facing the same problem here 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.

share|improve this answer

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.

share|improve this answer
    
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 '12 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 '12 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 '12 at 17:55

Preventing default behavior in IE and all other browsers:

event.returnValue = false;
if (event.preventDefault) event.preventDefault();
share|improve this answer
    
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 '12 at 9:32
    
This is for a DOM event. If Leaflet wraps events, use event.originalEvent. –  tmcw Nov 23 '12 at 3:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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