【问题标题】:Open image in new window在新窗口中打开图片
【发布时间】:2012-02-13 01:08:40
【问题描述】:

如何使用id 在新窗口中打开图片?

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

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

【问题讨论】:

    标签: javascript image


    【解决方案1】:

    对于一个很有可能在主窗口后面迷失的新窗口,并且通常会惹恼访问者:

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

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

    【讨论】:

    • 目前在 Chrome 上,我必须刷新才能看到图像。也使用绝对 URL
    • 为什么你会认为他正在处理面向客户的代码而不是调试工具或构建一个非常有用的功能以供特定用途?他问了一个抽象的问题。他应该得到一个抽象的答案。
    【解决方案2】:
    window.Open("http://yourdomain.com/yourimage.gif");
    

    【讨论】:

    • 您可能想要编辑您的答案。 window.Open 不是一种方法,如果您尝试使用它而不是 window.open,则会导致错误。
    【解决方案3】:

    类似

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

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

    【讨论】:

      【解决方案4】:
      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 代码:

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

      【讨论】:

        【解决方案5】:

        试试:

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

        【讨论】:

        • 我从 Chrome 收到此错误:Not allowed to navigate top frame to data URL
        【解决方案6】:

        试试下面的函数:

        function newTabImage() {
            var image = new Image();
            image.src = $('#idimage').attr('src');
        
            var w = window.open("",'_blank');
            w.document.write(image.outerHTML);
            w.document.close(); 
        }
        

        使用此 HTML 代码调用:

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

        【讨论】:

          【解决方案7】:

          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();
              }
          

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

          你必须把图片的网址放在图片标签中。

          【讨论】:

            猜你喜欢
            • 2012-07-25
            • 1970-01-01
            • 2014-03-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-04
            相关资源
            最近更新 更多