Turn a JavaScript local variable into a global variable

后端 未结 2 1528
感情败类
感情败类 2020-12-08 16:58

I have a JavaScript function to generate a variable. That function is activated by an onclick button event.

After that variable is generated, I need to use it as a gl

2条回答
  •  执念已碎
    2020-12-08 17:41

    You should be able to add the variable's value to a property of the global window object:

    window.yourVarName = yourVarName;
    

    Then the other functions will be able to access yourVarName simply by referencing yourVarname directly. There will be no need to use window.yourVarName.

    However keep in mind that in general, global variables are evil.

提交回复
热议问题