[英]Variable is not defined outside of the function
Can someone please help me with this?有人可以帮我吗? I am new and I debugged and tried several codes while executing my codes.我是新手,在执行我的代码时调试并尝试了几个代码。 I'm trying to pass a base64string into JotForms that will allow me to generate my image into their PDF. Here is my the error screenshot.我正在尝试将 base64 字符串传递到 JotForms 中,这样我就可以将我的图像生成到他们的 PDF 中。这是我的错误屏幕截图。 Error here此处出错
And this is the code I am having trouble with, this is my function.这是我遇到问题的代码,这是我的 function。
function screenshot() {
html2canvas(document.body, {
scrollY: -window.scrollY,
crossOrigin : 'Anonymous',
allowTaint: true,
foreignObjectRendering: true,
}).then(function (canvas) {
document.body.appendChild(canvas);
var data0101 = document.body.appendChild(canvas).toDataURL();
console.log('Result', data0101)
document.body.removeChild(canvas);
});
}
and this is the JotForm code that is outside the function,这是 function 之外的 JotForm 代码,
var myImage = data0101;
console.log(myImage)
var submissionData = {
valid: true,
value: JFCustomWidgetUtils.buildMetadata('imagelinks', [{
'name': "Data from Widget",
'base64': myImage
}])
}
JFCustomWidget.subscribe('ready', function (data333333) {
console.log("ready message arrived from JotForm", data333333);
JFCustomWidget.sendData(submissionData);//Do this as soon as your image is ready
JFCustomWidget.sendsubmit(submissionData);//This will run when the submit button is clicked
});
Thank you so much太感谢了
You declare your data0101 variable inside the funcion.您在函数内声明了data0101变量。 This means you can only use it inside your function. Once you leave this function, this variable no longer available.这意味着你只能在你的 function 内部使用它。一旦你离开这个 function,这个变量就不再可用。 Thats why you are getting this error.这就是您收到此错误的原因。
To solve this you can declare you variable before this function as a global variable.要解决这个问题,您可以在这个 function 之前将变量声明为全局变量。 Something like:就像是:
var data0101 = null;
function screenshot() {
html2canvas(document.body, {
scrollY: -window.scrollY,
crossOrigin : 'Anonymous',
allowTaint: true,
foreignObjectRendering: true,
}).then(function (canvas) {
document.body.appendChild(canvas);
data0101 = document.body.appendChild(canvas).toDataURL();
console.log('Result', data0101)
document.body.removeChild(canvas);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.