'leaflet.js check if a marker has its bound popup open

As per subject how can I check if a marker has a popup open?

eg.

var m = new L.marker([2,1]).addTo(map).bindPopup('test');

Clicking on the marker will obviously open the popup.

Is there any functionality such as:

if(m.popupOpen() == true) {
    // do somehting
}

I have tried the following:

m.on('click', function(e) {
    if(m._map.hasLayer(m._popup)) {
        // Do something
    }
}

But it's very flaky. On chrome it will run, on android browser if will fire on the second click so it is not really reliable.

Is there any standard way of doing this?

Thanks



Solution 1:[1]

Now 2022 using leaflet V.1.7.1 the popup object doesn´t have the _isOpen property anymore.

You can now use the method:

popup.isOpen()

https://leafletjs.com/SlavaUkraini/reference.html#popup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 john Smith