javascriptmako

Open image in new window in mako template


How can I open an image (with an url which is a mako variable) in a new window inside a mako template? I would like the simplest thing possible. This is what I'm trying to use:

<script>
function openImage(image_url) {
  window.open('_blank').document.write('<img src="' + image_url + '">');
}
</script>

<a href="#"><img src="${image_overview_url}" onclick="openImage(${image_to_open_url});"></a>

However, no new window opens with the code above... Any other options how to achieve this?


Solution

  • You might try putting quotes around your image url in your onclick attribute. I have replaced openImage(${image_to_open_url}) with openImage('${image_to_open_url}'). I don't think it can bare as you have it.

    <script>
    function openImage(image_url) {
      window.open('_blank').document.write('<img src="' + image_url + '">');
    }
    </script>
    
    <a href="#"><img src="${image_overview_url}" onclick="openImage('${image_to_open_url}');"></a>