0

I want to open images in new window but i cant figure out the JavaScript code!

HTML:

<img class="picture" onclick="expand()" src="bild1.jpg">

Javascript:

function expand()
{   
var bigImage = document.getElementByClassName('picture');

var source=bigImage.getAttribute('src');
window.open(source)             
}
  • refer to http://stackoverflow.com/questions/8908022/open-image-in-new-window http://stackoverflow.com/questions/13595003/opening-bigger-version-of-picture-in-new-window-w-javascript – swetha Apr 25 '14 at 09:53
  • 2
    At least you need to know, how [`getElementsByClassName`](http://stackoverflow.com/a/18481044/1169519) or at [MDN](https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName), works. – Teemu Apr 25 '14 at 09:54
  • Look at your JavaScript error console. Read the error messages. They'll make it pretty clear where you've screwed up. – Quentin Apr 25 '14 at 10:03

3 Answers3

0

Add the target="_blank" attribute to your links to open them in new windows, no javascript needed.

Ermir
  • 1,391
  • 11
  • 25
0

The target attribute specifies where to open the linked document..

You can use your image inside an anchor element.

<a target="_blank" href="yourimage.jpg"><img class="picture" src="yourimage.jpg"></a>

You dont need JS code

Yagiz Ozturk
  • 5,408
  • 7
  • 29
  • 44
0
<a href="bild1.jpg" target="_blank"><img class="picture"  src="bild1.jpg"></a>
Tuhin
  • 3,335
  • 2
  • 16
  • 27