简体   繁体   English

function 之外没有定义变量

[英]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.

相关问题 window 变量定义在 window function 外部看不到 - window variable defined in a window function not seen outside 在函数外部定义的javascript变量在函数内是“未定义的” - A javascript variable defined outside a function is 'undefined' within the function 在函数外部调用时,在函数中定义的全局变量显示未定义 - Variable global defined in a function is showing undefined when called outside the function 在 Moongoose 函数 NODE JS 之外未定义变量 id - Variable id not defined outside moongoose function NODE JS 定义函数时如何捕获外部变量的当前值 - How to capture current value of an outside variable when function is defined JavaScript - 如何获取外部onload函数中定义的变量? - JavaScript - How to take variable that defined inside onload function outside? 在 Puppeteer.evaluate() 外部定义的变量 Function 始终未定义 - Variable Defined Outside Puppeteer .evaluate() Function Always Undefined Javascript:我在函数中定义了全局变量,但无法在函数外部访问它。 为什么? - Javascript: I defined a global variable in function, but can't access it outside the function. why? 如何让 Javascript 记住在我的 function 中重置但最初在我的 function 之外定义的变量? - How do I get Javascript to remember a variable that is reset in my function, yet initially defined outside of my function? 功能变量未传递到外部 - Variable in function not passing to outside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM