简体   繁体   English

将变量从一个JS文件发送到另一个JS文件

[英]Sending variable from one JS file to another JS file

I have ABC.jsp file with the corresponding ABC.js file. 我有ABC.jsp文件和相应的ABC.js文件。 I also have XYZ.jsp file with the corresponding XYZ.js file. 我也有XYZ.jsp文件和相应的XYZ.js文件。 I have a variable VAR in ABC.js file. 我在ABC.js文件中有一个变量VAR。 I want to send that variable by Post method to the XYZ.js file so that I can use the variable VAR in XYZ.js for some computations . 我想通过Post方法将该变量发送到XYZ.js文件,以便可以将XYZ.js中的变量VAR用于某些计算。 These computations will help me in changing the content of XYZ.jsp file. 这些计算将帮助我更改XYZ.jsp文件的内容。

I want to send the variable VAR in a javascript function (obviously in ABC.js file) which gets called when a button on ABC.jsp is clicked. 我想在单击ABC.jsp上的按钮时被调用的javascript函数中发送变量VAR(显然在ABC.js文件中)。

So, can anyone please help me in sending the variable from ABC.js to XYZ.js by post method. 因此,任何人都可以帮助我通过post方法将变量从ABC.js发送到XYZ.js。 And also how to retrieve the variable in XYZ.js file. 以及如何在XYZ.js文件中检索变量。

I'm novice in this area. 我是这方面的新手。 Any help will be much helpful for me. 任何帮助都会对我有很大帮助。

make your ABC.js script with the variable to pass it by GET method to XYZ.jsp page 使用变量使您的ABC.js脚本通过GET方法将其传递到XYZ.jsp页面

XYZ.jsp?variable=1234

than, when XYZ.jsp gets rendered with 'variable' parameter passed, it will render this variable somewhere... even as a global JavaScript variable. 相比之下,当XYZ.jsp通过传递'variable'参数来呈现时,它将在某个位置呈现此变量...甚至作为全局JavaScript变量。

// this is going to be rendered in JSP where you just assign the value from the parameter passed to the JSP page //这将在JSP中呈现,您只需在传递给JSP页面的参数中分配值即可

<script>
    myVariable="1234";   // in JPS   myVariable=\"${param.variable}\";
</script>

read myVariable from you ABC.js script 从您的ABC.js脚本中读取myVariable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM