2

Can JavaScript be included in a leaflet popup?

We have modified this example

To this :-

var this_marker = L.marker([51.5, -0.09]).addTo(mymap).bindPopup(
 '<div style="color: red">Hello<br>world<script>console.log("testing");
  </script></div>').openPopup();

This gives an error.

In our application we see the correctly formed HTML with script tags but the JavaScript does not run.

3
  • It would help if you put up a minimal code example in jsFiddle or similar. Commented May 14, 2016 at 14:38
  • 1
    This post seems similar to this question: stackoverflow.com/questions/13698975/…
    – Qwerty
    Commented May 14, 2016 at 15:44
  • Do you want to execute code right after the popup has been opened? Commented Jun 7, 2016 at 9:41

1 Answer 1

1

Your code above works great without the <script> tag once its removed, like so:

var this_marker = L.marker([51.5, -0.09]).addTo(mymap).bindPopup
('<div style="color: red">Hello<br>world</div>').openPopup();

If you are trying to add in additional JavaScript functionality to the popup when its open you could use a selector such as:

mymap.on('popupopen', function() {
    console.log(testing);    
});

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.