Javascript 在新窗口中打开图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8908022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:02:37  来源:igfitidea点击:

Open image in new window

javascriptimage

提问by Khush

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

如何使用它在新窗口中打开图像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?

单击图像时调用此函数。现在,它在同一个窗口中打开,但我想在新窗口中打开它。如何做到这一点?

回答by Umesh Patil

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:

HTML代码:

<img src="abc.jpg" onClick="swipe();"/>

回答by Quentin

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.

如果我是你,我会坚持使用常规链接。

回答by Rodolfo Jorge Nemer Nogueira

Try:

尝试:

<img src="URL or BASE64" onclick="window.open(this.src)">

回答by Alexander Menger

HTML:

HTML:

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

JavaScript

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/

试试这个:https: //jsfiddle.net/ne6f5axj/10/

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

您必须将图像的 url 放在图像标签中。

回答by basti

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.

但是,如果有人使用 AdBlock 或任何 PopUp-Blocker,您可能会遇到麻烦。

回答by mikemaal

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:

使用此 HTML 代码调用:

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

回答by Alex Dn

window.Open("http://yourdomain.com/yourimage.gif");