Question

How do I have JavaScript open the current image in a new WINDOW with an ONCLICK event.

<script>
 function imgWindow() {
  window.open("image") }
</script>

HTML

<img src="pond1.jpg" height="150" size="150" alt="Johnson Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
<img src="pond2.jpg" height="150" size="150" alt="All-green Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
<img src="pond3.jpg" height="150" size="150" alt="Dutch Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
Was it helpful?

Solution

here you go.

<img src="https://i.imgur.com/7KpCS0Y.jpg" onclick="window.open(this.src)">

OTHER TIPS

Developers also take care about accessibility.

Do not use onClick on images without defining the ARIA role.

Non-interactive HTML elements and non-interactive ARIA roles indicate content and containers in the user interface. A non-interactive element does not support event handlers (mouse and key handlers).

The developer and designers are responsible for providing the expected behavior of an element that the role suggests it would have: focusability and key press support. More info see WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets.

tldr; this is how it should be done:

  <img
    src="pond1.jpg"
    alt="pic id code"
    onClick="window.open(this.src)"
    role="button"
    tabIndex="0"
  />

This might work for you...

<script type="text/javascript">
function image(img) {
    var src = img.src;
    window.open(src);
}
</script>
<img src="pond1.jpg" height="150" size="150" alt="Johnson Pond" onclick="image(this)">

use this simple cod:

    <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}

#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

/* Caption of Modal Image */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {-webkit-transform:scale(0)} 
  to {-webkit-transform:scale(1)}
}

@keyframes zoom {
  from {transform:scale(0)} 
  to {transform:scale(1)}
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
</style>
</head>
<body>

<h2>Image Modal</h2>
<p>In this example, we use CSS to create a modal (dialog box) that is hidden by default.</p>
<p>We use JavaScript to trigger the modal and to display the current image inside the modal when it is clicked on. Also note that we use the value from the image's "alt" attribute as an image caption text inside the modal.</p>

<img id="myImg" src="img_snow.jpg" alt="Snow" style="width:100%;max-width:300px">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

<script>
// Get the modal
var modal = document.getElementById("myModal");

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("modal")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
</script>

</body>
</html>

this code open and close your photo.

I think your error was in calling the function.

In your HTML code, onclick is calling the image() function. However, in your script the function is named imgWindow(). Try changing the onclick to imgWindow().

I don't do much JavaScript so if I have missed something, please let me know.

Good Luck!

As mentioned in my comment to @Ben, the accepted solution does not seem to work for data:URL images.

Sometimes the underlying intent needs to be revisited. In my case, I wanted the visitor to be able to see the image as large as possible on her screen.

If this is your intent, here's a solution:

  • Key idea: inject a window filling div into the DOM and put the image data into the background-image CSS style
  • The code:

// the the click/tap handler to maximize the small image 
var Min = document.getElementById('Min');
if (Min){
  Min.addEventListener('click', function(){
  
    // transfer the image data
    Max.style.backgroundImage="url(" + this.src + ")";
    
    // make the large image appear
    Max.style.display="block";
  });
}

// the the click/tap handler to minimize the large image 
var Max = document.getElementById('Max')
if (Max){
  Max.addEventListener('click', function(){

    // make the large image disappear
    Max.style.display="none";
  });
}
#Min {
  /* just for fun */
  cursor: zoom-in;
}
#Max {
  /* fill the window */
  position: fixed;
  top:0;
  bottom:0;
  left:0;
  right:0;
  
  /* prepare for the precious load */
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
  
  /* make sure the environment is ready to dominate the scene */
  z-index: 10000;
  background-color:#fff;
  
  /* div is initially hidden */
  display:none;
  
  /* the fun never ends */
  cursor: zoom-out;
}
<!-- src can be an image URL as well! -->
<img id="Min" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAIAAACzY+a1AAAABmJLR0QA/wD/AP+gvaeTAAAC7ElEQVR4nO3dwWrkMBAA0Tjk/3/ZOeQQEUaZFt0tq6DecZn1eii0Qh7Zvu77/hDZ59MnoCwT4pkQz4R4JsQzIZ4J8UyIZ0I8E+KZEM+EeCbEMyGeCfFMiGdCPBPimRDPhHgmxDMhngnxTIj3VXis67oKj/Zj3Kk8O35kN/P4d2efn32m+3slOQrxTIhnQrzKuXCU+b8+M+fNzOa21WN2fK8kRyGeCfFMiNc1F44ic0DH2i7yb2Xmp6rvleQoxDMhngnxdsyFHSLz4mzO61h3PshRiGdCPBPiUefCUdVaMHPt9EGOQjwT4pkQb8dcWDWvrP7m13299JD50lGIZ0I8E+J1zYUd+0Qi+zxXr5dmzuEQjkI8E+KZEO86ZHGzKjMvrh7zcI5CPBPimRCvci6susbYMc9Fjp/5zEzH+f/hKMQzIZ4J8brWhd3rtlHVV4gcf/X8N6w1HYV4JsQzIV7Xc2cy+zZXf/9bPbfIcTr2+zRxFOKZEM+EeE9eI52pWod1WL1e6lyo90yIZ0K8rrkw8xzRzO+Ombmw6v6Kzc+jcRTimRDPhHi795FWrfNW59oT5jb3zug1E+KZEG/H/YVV9zbMnqOW2Qu6eg6zz0T+3L0zes2EeCbE27F3ZhT5LTDymcxz1Kre/TTTMTf/w1GIZ0I8E+KdeK99x7qq4zpqhnOhfpkQz4R4Jz6bO7Oei6w1M+cwO5/MWjbJUYhnQjwT4pHea9/xjJjuPS8b1pqOQjwT4pkQj/Re+8xxVt9lOMqsKZ0L9Z4J8UyIR3qvfccxM3Pk7Hwi+4AKOQrxTIhnQrwTfy+cicwlVb/hVe1l3cBRiGdCPBPikebCGcpzZ7y/UK+ZEM+EeKT32lcdc+d7Cn0eqd4zIZ4J8UjvtY/ofr/E6nXRDWtERyGeCfFMiHfivfZa4ijEMyGeCfFMiGdCPBPimRDPhHgmxDMhngnxTIhnQjwT4pkQz4R4JsQzIZ4J8UyIZ0I8E+KZEM+EeN9J+oQ5rikYTQAAAABJRU5ErkJggg==" />

<div id="Max"></div>

This code works both for image files and data:URLs.

Any improvement suggestions?

Instead of all that just us 'a-tag'

<a href="https://www.google.com/">
    <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" draggable="false"/>
</a>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top