简体   繁体   English

如何将一个 javascript 文件中的值传递到另一个 javascript 文件中

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

相关问题 将变量从一个javascript文件传递到另一个javascript文件 - pass a variable from one javascript file to another javascript file 如何将 javascript 变量从一个 javaScript 文件传递到另一个 javascript 文件? - how to pass javascript variables from one javaScript file to another javascript file? 如何将数据从一个Javascript控制器文件传递到另一个? - How to pass data from one Javascript controller file to another? 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何在 javascript 中将 object 从一个文件传递到另一个文件? - How to pass object from one file to another in javascript? 当我在函数中传递值时,如何将javascript上的值传递给另一个javascript文件? - How to pass value from on javascript to another javascript file when I pass value in function? 将值从一个文件传递到另一个javascript - passing a value from one file to another javascript 如何将值从javascript传递到php文件 - How to pass value from javascript to php file 如何从一个javascript文件到另一个javascript文件的原型方法获取原型方法的价值? - How to get value of a prototype method from one javascript file to another javascript file's prototype method? 如何从另一个加载一个JavaScript文件? - How to load one JavaScript file from another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM