Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught (in promise) Provided element is not within a Document #1313

Closed
cjcortez opened this issue Dec 14, 2017 · 23 comments
Closed

Uncaught (in promise) Provided element is not within a Document #1313

cjcortez opened this issue Dec 14, 2017 · 23 comments

Comments

@cjcortez
Copy link

What is the meaning of this error? and how to fix it

@niklasvh
Copy link
Owner

The element you are trying to render is not within the Document DOM

@cjcortez
Copy link
Author

So how to fix this issue?

0.5 is fine but when I upgrade to 1 I got this error

@niklasvh
Copy link
Owner

Please share an example on jsfiddle

@RaghavPrabhu
Copy link

I have tried to load the html2canvas js (version 1) and my app js after the HTML body and this issue got fixed for me.

@Digambarmalla
Copy link

Digambarmalla commented Dec 15, 2017

Does anyone got a fix for this issue, I have the same problem while using in ASP.Net MVC cshtml page.

@smartbepl
Copy link

If you use jQuery to find DIV use
html2canvas($("#element")[0]).then(function(canvas) {
$("#element-out").append(canvas);
});

@Digambarmalla
Copy link

Digambarmalla commented Dec 17, 2017

But I am unable to take image of google map using html2canvas(all map image is not coming), Can anyone help me out how to do the same in cshtml ???

@niklasvh
Copy link
Owner

@cjcortez @RaghavPrabhu does smartbepl's solution above fix it for you presuming you are using jQuery or some other library that provides a list of elements instead of a single Element?

@jeremielodi
Copy link

This works for me!
html2canvas($('#div').get(0)).then( function (canvas) {
console.log(canvas);
});

@m2shash
Copy link

m2shash commented Dec 27, 2017

thanks it worked

@lorkel
Copy link

lorkel commented Dec 29, 2017

@jeremielodi Thank you, this was driving me nuts!

@schrepel
Copy link

schrepel commented Jan 5, 2018

I tried code similar to the above, but maybe the error I'm getting isn't quite related: Uncaught (in promise) undefined... Promise rejected (async)

@ikemike
Copy link

ikemike commented Jan 25, 2018

I was having the exact same problem - I wanted to call HTML2Canvas on button click. No matter how I wrote the HTML2Canvas call, I would get an "Uncaught (in promise) undefined...Promise rejected (async)" error. Finally, I learned a little bit about how promises work and found the solution was adding a catch:

function myFunction() {
    html2canvas(document.querySelector("#capture")).then(canvas => {          
        	var base64encodedstring = canvas.toDataURL("image/jpeg", 1).replace("data:image/jpeg;base64,", "");
        	j$("[id$='inputHidden']").val(base64encodedstring);
        	console.log('Saving...');
        	mySaveFunction();
        })
    	.catch(function (error) {
        	/* This is fired when the promise executes without the DOM */    
    	});
}

Word of warning, I'm a total javascript novice and understand very little about how promises work. My code worked fine without the .catch() if I used it outside of a function. I'm assuming that somehow, when you encapsulate it, it no longer has the correct DOM access and the promise fails.

@jeremielodi
Copy link

jeremielodi commented Jan 26, 2018

@ikemike this can help you

<html>
<head>
  <title>html2canvas</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
  <style type='text/css'>
   #test {
     height: 40px;
     width: 40px;
     border: 1px solid #001;
     background: red
   }
 </style>

</head>
<body>

  <div id='test' >
  </div>

  <img src='' alt='' id='img' />

  <script>
    html2canvas($('#test').get(0)).then(function (canvas) {
      var base64encodedstring = canvas.toDataURL("image/jpeg", 1);
      $('#img').attr('src', base64encodedstring);
    });
  </script>
</body>
</html>

@bandacs
Copy link

bandacs commented Mar 29, 2018

@niklasvh with latest version of html2canvas I am getting Uncaught (in promise): undefined error. Can you please help me.

image

image

@yihuiluo235
Copy link

ths,it works

@leandrocgsi
Copy link

This works fine for me:

function downloadURI(uri, name) {
    var link = document.createElement("a");

    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();
    clearDynamicLink(link); 
}

function DownloadAsImage() {
    var element = $("#table-card")[0];
    html2canvas(element).then(function (canvas) {
        var myImage = canvas.toDataURL();
        downloadURI(myImage, "cartao-virtual.png");
    });
}

@joniel
Copy link

joniel commented May 21, 2018

in chrome, it works fine, but in IE11, doesn't work... T_T;

Uncaught (in promise): undefined error

@impactcolor
Copy link

@bandacs did you find a solution to this? I'm getting exact error as your screen shot.

@impactcolor
Copy link

impactcolor commented Sep 28, 2018

I'm using 1.0 alpha 12 release and I'm having the same problem. I've tried all of these solutions.

I've tried this solution by @smartbepl
html2canvas($("#element")[0]).then(function(canvas) {
$("#element-out").append(canvas);
});

I tried this by @jeremielodi
html2canvas($('#div').get(0)).then( function (canvas) {
console.log(canvas);
});

I also tried leandrocgsi solution and tried adding a catch as @ikemike suggest.

To simplify it and ensure all elements are loaded on the DOM, I place a function called take_screenshot().

function take_screenshot()
{
html2canvas($(".image__container")[0]).then(canvas => {
console.log('please work I am loosing my mind');
});
}
I then call take_screenshot() directly from the javascript console to ensure everything loaded.
I get the same error Uncaught (in promise) undefined.

@rashidhamid21
Copy link

rashidhamid21 commented May 16, 2019

I have exactly the same issue that faced by #1313 (comment)
Can anyone found solution on this issue?

@hhhluke
Copy link

hhhluke commented Jul 1, 2019

why i failed in cheerio?

 superagent
   .get(url)
   .end((err, res) => {
       cheerio.load(res.text)
       html2Canvas($('#statuses').get(0), {
           allowTaint: true
       }).then(function(canvas) {})
})
Uncaught (in promise) Error: Element is not attached to a Document

@1der1
Copy link

1der1 commented Nov 10, 2019

There is another calling convention now.
Version 0.5 expected the node in an array, while you now give it directly.
Version 0.5 had the "onrendered" option, while you now use the "then" construct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests