19

How can I open an image in a new window by using its id?

function swipe()
{   
    var largeImage = document.getElementById('largeImage');
    largeImage.style.display = 'block';
    largeImage.style.width=200+"px";
    largeImage.style.height=200+"px";                   
}

This function is called on click on the image. Right now, it's opening in the same window, but I want to open it in a new window. How can this be accomplished?

Khush
  • 867
  • 1
  • 19
  • 46

7 Answers7

30
function swipe() {
   var largeImage = document.getElementById('largeImage');
   largeImage.style.display = 'block';
   largeImage.style.width=200+"px";
   largeImage.style.height=200+"px";
   var url=largeImage.getAttribute('src');
   window.open(url,'Image','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1');
}

HTML code:

<img src="abc.jpg" onClick="swipe();"/>
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Umesh Patil
  • 10,475
  • 16
  • 52
  • 80
16

For a new window that has a good chance of being lost behind the main window, and generally annoying visitors:

window.open('http://example.com/someImage.png');

I'd just stick to a regular link if I were you.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Currently on Chrome, I have to refresh to see the image. Using absolute URL too – TheRealChx101 May 09 '15 at 04:25
  • 7
    Why would you assume he's working on customer-facing code and not debugging tools or building an extremely useful feature for specific use? He asked an abstract question. He deserves an abstract answer. – JayB Nov 20 '16 at 00:53
9

Try:

<img src="URL or BASE64" onclick="window.open(this.src)" style="cursor: pointer;">
jrreda
  • 763
  • 7
  • 7
5

Try with the following function:

function newTabImage() {
    var image = new Image();
    image.src = $('#idimage').attr('src');

    var w = window.open("",'_blank');
    w.document.write(image.outerHTML);
    w.document.close(); 
}

Call with this HTML code:

<img id="idimage" src="data:image/jpg;base64,/9j/4A.." onclick="newTabImage()">
mikemaal
  • 317
  • 3
  • 6
3

HTML:

<input type="button" onclick="test()" value="test">

JavaScript

    function test(){
    url = "https://www.google.de//images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
    img = '<img src="'+url+'">';
    popup = window.open();
    popup.document.write(img);                        
    popup.print();
    }

Try this: https://jsfiddle.net/ne6f5axj/10/

You have to put the url of image in an image-tag.

0

Something like

window.open(url,'htmlname','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1');}

But you might run in trouble, if someone uses AdBlock or any PopUp-Blocker.

basti
  • 2,649
  • 3
  • 31
  • 46
-6
window.Open("http://yourdomain.com/yourimage.gif");
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
  • 2
    You may want to edit your answer. `window.Open` isn't a method and will cause an error if you try to use it instead of `window.open`. – Chris Schmitz May 06 '16 at 15:33