简体   繁体   English

从外部JS文件导入变量值

[英]Import variable values from an external JS file

I have a JS file located in directory c:\\myJSfiles\\FileNumberOne.xyz 我在目录c:\\ myJSfiles \\ FileNumberOne.xyz中有一个JS文件

    //c:\myJSfiles\FileNumberone.xyz 

    var 1 = ThisValue1;
    var 2 - ThisValue2;
    and so on

In the same root directory, I have second JS file c:\\myJSfiles\\FileNumberTwo.xyz which I want to import the values of the variables from FileNumberOne.xyz 在同一根目录中,我有第二个JS文件c:\\ myJSfiles \\ FileNumberTwo.xyz,我想从FileNumberOne.xyz导入变量的值

    //file: c:\myJSfiles\FileNumberTwo.xyz

    var A = "c:\myJSfiles\FileNumberOne.xyz", var 1;
    var B = "c:\myJSfiles\FileNumberOne.xyz", var 2;

How would I do this? 我该怎么做?

var 1 makes no sense, I assume you did it as an example only. var 1没有任何意义,我认为您只是作为示例而已。 Otherwise if you declare something in the first referenced script on the page, it will automatically be available in the second. 否则,如果您在页面上第一个引用的脚本中声明了某些内容,则第二个中将自动提供该内容。

for example if you have in 例如,如果您有

<script src="Script1.js"></script>

line 线

var something = "hello"

you can reference the second script after the first 您可以在第一个脚本之后引用第二个脚本

<script src="Script2.js"></script>

and in that script use that variable 在该脚本中使用该变量

alert(something); // will display hello

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

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