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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27355397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:04:12  来源:igfitidea点击:

How to get variable value from another js file

javascriptvariablesglobal

提问by prabin badyakar

I've JavaScript file named Second.jswhich contains variable named page.

我有一个名为 JavaScript 的文件Second.js,其中包含名为page.

Second.js:

第二个.js:

var page = 1;

How can I retrieve that value, i.e page, from another JavaScript named one.js?

如何page从另一个名为 的 JavaScript 中检索该值one.js

回答by Shah Nawaz

An other way to get a value from another .js file is saving the variable in the browser:

从另一个 .js 文件获取值的另一种方法是将变量保存在浏览器中:

a.js
var myemail="[email protected]";
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) // [email protected]

回答by cherouvim

If you really want variable visibility between different scripts you can attach the variable on the window. e.g:

如果您真的想要不同脚本之间的变量可见性,您可以将变量附加到window. 例如:

script 1:

脚本1:

window.page = 1;

script 2:

脚本2:

alert(page);

Make sure you don't use very simple variable names (e.g such as page) to avoid possible collisions. 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". e.g:

确保不要使用非常简单的变量名(例如,如page)以避免可能的冲突。一个好的做法是在窗口上附加一个应用程序名称数组并使用它,确保不会影响“全局范围”上的任何内容。例如:

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

回答by T.J. Crowder

All JavaScript on the page executes in the same overall environment, so if pageis a global in Second.js, you can use it from any other JavaScript you load on the page. E.g.:

页面上的所有 JavaScript 都在相同的整体环境中执行,因此如果page是全局 in Second.js,则可以从加载到页面上的任何其他 JavaScript 中使用它。例如:

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

Provided, again, that pageis a global, you can use it from SomeOtherFile.js(just referencing it as page, no qualifiers).

再次提供,这page是一个全局变量,您可以使用它 from SomeOtherFile.js(仅将其引用为page,没有限定符)。

Or even:

甚至:

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

If pageisn'ta global, then you'll have to modify Second.jsin some way to make the pagevariable or value available to other scripts. 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.)

如果page不是全局变量,则您必须以Second.js某种方式进行修改以使page变量或值可用于其他脚本。当然,您可以通过将其设置为全局来实现,或者您可以使用异步模块定义加载器来避免周围有一堆全局变量并管理依赖项。(对于您正在做的事情,AMD 可能有点矫枉过正,或者不是,我们这里没有很多背景信息。)

回答by Michael Voznesensky

They both have to be included into the same HTML file. Make sure both are in, and loaded, and then it is as if all your javascript exists in one single, massive, JS file!

它们都必须包含在同一个 HTML 文件中。确保两者都在并加载,然后就好像您的所有 javascript 都存在于一个单一的、庞大的 JS 文件中!

回答by cheersphilip

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”重新声明它们。