[英]how to pass a value from one javascript file into another javascript file
Currently, by using the "$.getScript" jquery method I am able to pass the pass parameters from one javascript file into another javascript file.目前,通过使用“$.getScript”jquery 方法,我能够将传递参数从一个 javascript 文件传递到另一个 javascript 文件。 For instance, this $.getScript call made on javascript file name "secondJavaScript.js" file to call the function name callMemoPage() from "firstJavaScript.js" file.
例如,对 javascript 文件名“secondJavaScript.js”文件进行的 $.getScript 调用以从“firstJavaScript.js”文件中调用 function 名 callMemoPage()。 I am passing the following "pageName", "acctNum" paramaters from "secondJavaScript.js" into "firstJavaScript.js" file.
我将以下“pageName”、“acctNum”参数从“secondJavaScript.js”传递到“firstJavaScript.js”文件中。 However, the issue how to get the value of the function "callMemoPage" into this "secondJavascript.js" file.
但是,如何将 function“callMemoPage”的值获取到这个“secondJavascript.js”文件中的问题。 And that value kept in variable "Memo" - which is in "firstJavaScript.js" file.
该值保存在变量“Memo”中 - 在“firstJavaScript.js”文件中。
$.getScript('firstJavaScript.js', function(){
getMemo();
function getMemo(){
var pageName = "Memo Page"
var acctNum = "123"
// callMemoPage function located in "firstJavaScript.js" file
callMemoPage(pageName, acctNum)
console.log(Memo);
}
});
// firstJavaSctipt.js file
function callMemoPage(pageName, acctNum){
var Memo;
if(pageName == 'Memo Page') && (acctNum = '123'){
function(memoField){
var memoField = window.open("https://www.example.com");
// question how to pass the value of Memo into secondJavaScript.js file
Memo = $(memoField).find('input[name="MEMO"]').val();
}
}
}
You can write it to a JSON file using the fs module:您可以使用fs模块将其写入JSON文件:
const fs = require('fs');
fs.writeFileSync('file.json', JSON.stringify(yourVariable));
// Other file
const myVariable = require('file.json');
If these two pages are in the same origin: see https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy , you can store the result of Memo
in a cookie and then access it in the second page如果这两个页面在同一来源:请参阅https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy ,您可以将
Memo
的结果存储在 cookie 中,然后在第二页
Let's say you have multiple functions in fileOne.js, and you want to use them in fileTwo.js.假设您在 fileOne.js 中有多个函数,并且您想在 fileTwo.js 中使用它们。 well in HTML you import them like this:
在 HTML 中,您可以像这样导入它们:
<html>
...
<script src="fileOne.js">
<script src="fileTwo.js">
...
</html>
Notice that fileOne.j s must be before fileTwo.js , so every variable in fileOne.js will be available in fileTwo.js.请注意fileOne.js必须在fileTwo.js之前,因此 fileOne.js 中的每个变量都将在 fileTwo.js 中可用。 That's because HTML is synchronous .
那是因为 HTML 是同步的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.