最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html2canvas - no screenshot for iframe - Stack Overflow

programmeradmin2浏览0评论

I have a task where i need to load a URL (e.g www.yahoo) , on my webpage, and take screenshot. I am using html2canvas for screenshot and appending it to the body of the page.

The page specified by the URL is successfully loaded in an iframe inside a div element. But when i try to take screenshot of that, the iframe area comes blank.

Below is the code for previewURL and screenshot.

//to preview the URL content
function previewUrl(url,target){
    //use timeout coz mousehover fires several times
    clearTimeout(window.ht);
    window.ht = setTimeout(function(){
    var div = document.getElementById(target);
    div.innerHTML = '<iframe style="width:100%;height:100%;" frameborder="0" src="' + url + '" />';
    },20);      
}

function pic() {
      html2canvas(document.body, {
      onrendered: function(canvas) {
          document.body.appendChild(canvas);
         }
     });
 };

And the HTML part goes here :

<body>
    <input type="button" class="clear-button" onclick="pic();" value="Take Screenshot" >
    <a href="" onmouseover="previewUrl(this.href,'div1')">Hover to load</a>
    <div id="div1"></div>

</body>

The screenshot looks something like this :

I am stuck and don't understand why is this happening. I want something similar to this which can load URL and then onclick can give me screenshot.

I have a task where i need to load a URL (e.g www.yahoo.com) , on my webpage, and take screenshot. I am using html2canvas for screenshot and appending it to the body of the page.

The page specified by the URL is successfully loaded in an iframe inside a div element. But when i try to take screenshot of that, the iframe area comes blank.

Below is the code for previewURL and screenshot.

//to preview the URL content
function previewUrl(url,target){
    //use timeout coz mousehover fires several times
    clearTimeout(window.ht);
    window.ht = setTimeout(function(){
    var div = document.getElementById(target);
    div.innerHTML = '<iframe style="width:100%;height:100%;" frameborder="0" src="' + url + '" />';
    },20);      
}

function pic() {
      html2canvas(document.body, {
      onrendered: function(canvas) {
          document.body.appendChild(canvas);
         }
     });
 };

And the HTML part goes here :

<body>
    <input type="button" class="clear-button" onclick="pic();" value="Take Screenshot" >
    <a href="http://www.yahoo.com" onmouseover="previewUrl(this.href,'div1')">Hover to load</a>
    <div id="div1"></div>

</body>

The screenshot looks something like this :

I am stuck and don't understand why is this happening. I want something similar to this which can load URL and then onclick can give me screenshot.

Share Improve this question asked Sep 11, 2013 at 9:15 roger_thatroger_that 9,79119 gold badges70 silver badges110 bronze badges 3
  • 2 as canvas is subject to same origin policy, you have to proxify it server side, just like your posted link works – A. Wolff Commented Sep 11, 2013 at 9:26
  • How do we achieve that? An example or something would be beneficial. – roger_that Commented Sep 11, 2013 at 9:45
  • 2 You can't do that with html2canvas. It says so in the documentation. – Matt Ellen Commented Jun 13, 2014 at 11:27
Add a comment  | 

3 Answers 3

Reset to default 8

The problem here is that you are not pointing correctly to the part of the iframe that you want to take the screenshot, instead you are pointing directly to the document body.

you can try this:

var body = $(iframe).contents().find('body')[0];
        html2canvas(body, {
            onrendered: function( canvas ) {
                $("#content").empty().append(canvas);
            }, 

Hope this helps!

Seems like it's not possible:

The script doesn't render plugin content such as Flash or Java applets. It doesn't render iframe content either.

http://html2canvas.hertzen.com/documentation.html#limitations

This code worked 4 me:

setTimeout(() => {
    html2canvas($('#'+idd2).contents().find('body')[0], {
        allowTaint : true,
        logging: true,
        profile: true,
        useCORS: true
        }).then(function(canvas) {
        document.getElementById('screen').appendChild(canvas);     
    }); }, 3000);
发布评论

评论列表(0)

  1. 暂无评论