简体   繁体   English

如何从另一个js文件获取变量值

[英]How to get variable value from another js file

I've JavaScript file named Second.js which contains variable named page . 我有一个名为Second.js JavaScript文件,其中包含名为page变量。

Second.js: Second.js:

var page = 1;

How can I retrieve that value, ie page , from another JavaScript named one.js ? 如何从另一个名为one.js JavaScript中检索该值(即page

An other way to get a value from another .js file is saving the variable in the browser: 从另一个.js文件获取值的另一种方法是将变量保存在浏览器中:

a.js
var myemail="xyz@gmail.com";
localStorage.setItem("email",myemail);

Now this variable is saved in browser. 现在,此变量保存在浏览器中。 If we want to get this variable in another file we use: 如果要在另一个文件中获取此变量,请使用:

b.js
var x=localStorage.getItem("email");
alert(x) // output=xyz@gmail.com

If you really want variable visibility between different scripts you can attach the variable on the window . 如果您确实希望变量在不同脚本之间可见,则可以在window上附加变量。 eg: 例如:

script 1: 脚本1:

window.page = 1;

script 2: 脚本2:

alert(page);

Make sure you don't use very simple variable names (eg such as page ) to avoid possible collisions. 确保不要使用非常简单的变量名(例如page ),以避免可能的冲突。 A good practice is to attach an array of your application name on the window and work with that, being sure that you'll not affect anything on the "global scope". 一个好的做法是在窗口上附加一个应用程序名称数组,并使用它,并确保您不会影响“全局范围”中的任何内容。 eg: 例如:

window.MY_APP = {
  page: 1,
  foo: 'bar',
  whatever: [1, 2, 3],
  example: {
    name: 'john',
    surname: 'smith'
  }
};

All JavaScript on the page executes in the same overall environment, so if page is a global in Second.js , you can use it from any other JavaScript you load on the page. 页面上的所有JavaScript都在相同的整体环境中执行,因此如果Second.js page是全局的,则可以从页面上加载的任何其他JavaScript中使用它。 Eg: 例如:

<script src="Second.js"></script>
<script src="SomeOtherFile.js"></script>

Provided, again, that page is a global, you can use it from SomeOtherFile.js (just referencing it as page , no qualifiers). 再次提供该page是全局的,您可以从SomeOtherFile.js使用它(只是将其引用为page ,没有限定符)。

Or even: 甚至:

<script src="Second.js"></script>
<script>
    alert(page); // "1"
</script>

If page isn't a global, then you'll have to modify Second.js in some way to make the page variable or value available to other scripts. 如果page 不是全局的,则必须以某种方式修改Second.js ,以使page变量或值可用于其他脚本。 You can do that by making it global, of course, or you can use an Asynchronous Module Definition loader to avoid having bunches of globals around and manage dependencies. 当然,您可以通过将其设置为全局来做到这一点,或者可以使用“异步模块定义”加载程序来避免周围有大量的全局变量并管理依赖项。 (AMD may be overkill for what you're doing, or not, we don't have a lot of context here.) (AMD可能会因您的所作所为而变得过大,或者,我们在这里没有太多背景信息。)

They both have to be included into the same HTML file. 它们都必须包含在同一个HTML文件中。 Make sure both are in, and loaded, and then it is as if all your javascript exists in one single, massive, JS file! 确保两者都已装入并已加载,然后就好像您的所有JavaScript都存在于一个单独的大型JS文件中一样!

There are two ways of doing this: load the scripts in the order you want to access them, or don't start executing your scripts until the page the has loaded (recommended, as this ensures that everything is definitely loaded!). 有两种方法:按照要访问脚本的顺序加载脚本,或者在加载页面之前不要开始执行脚本(建议这样做,因为这样可以确保确实加载了所有内容!)。 Either way you can reference your variables as if they were in the same file - you do not need to re-declare them with 'var'. 无论哪种方式,您都可以像在同一文件中一样引用变量-无需使用'var'重新声明它们。

暂无
暂无

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

相关问题 从select获取变量值,并将其传递到另一个JS文件 - get a variable value from select and pass this to another JS file 如何从另一个方法获取变量的值? (vue.js 2) - How to get value of variable from method another ? (vue.js 2) 如何从 js 文件上的一个 function 获取变量并将其用于另一个 js 文件上的另一个 function? - How do I get a variable from one function on a js file and use it for another function on another js file? 如何使用jQuery从另一个.js文件获取var值 - How to get var value from another .js file with jQuery 如何从 python 代码获取值到 JS 文件中的 Javascript 变量 - how to get a value from a python code to a Javascript variable in JS file 如何从一个文件到另一个js文件中获取一个js函数的值 - how to get the value of one js functions from one file to another js file 如何将全局变量值从js文件中的函数传递到其他文件中的另一个函数 - How to pass a global variable value from a function in a js file to another function in a different file 将变量的值从一个js文件传递到另一个 - Passing the value of a variable from one js file to another 从一个JS文件获取变量的值到另一个 - Getting value of variable from one JS file to another 在 express js 中,如何从另一个 api 响应的文件中读取内容并将内容存储在变量中? - in express js how to read content from a file that i get as a response from another api and store the content in a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM