Javascript:.js 文件之间共享的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8416076/
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-24 05:55:43  来源:igfitidea点击:

Javascript: Global variables shared between .js files

javascripthtmlglobal-variables

提问by vctlzac

I'm having problems with global variables.

我在使用全局变量时遇到问题。

Considering that I have the following files: init.html, main.html, init.js, main.js and help.js :

考虑到我有以下文件:init.html、main.html、init.js、main.js 和 help.js:

Where, init.html:

在哪里,init.html:

<HTML>
   <HEAD>
   <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
   <script type="text/javascript" charset="UTF-8" src="init.js" ></script>
   <script type="text/javascript" charset="UTF-8" src="main.js" ></script>
   <script type="text/javascript" charset="UTF-8" src="help.js" ></script>

   </HEAD>
   <BODY>
       <script>
          $(document).ready(function() {
          test();
        });
       </script>
  </BODY>
</HTML>

In init.js :

在 init.js 中:

function test(){
alert(window.glob);
}

In main.html :

在 main.html 中:

<HTML>
  <HEAD>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"> </script>
      <script type='text/javascript' > 
          top.glob = "global variable";
      </script>
      <script type="text/javascript" charset="UTF-8" src="help.js" ></script>   
      <script type="text/javascript" charset="UTF-8" src="main.js" ></script>
  </HEAD>
  <BODY>    
     <div id="divtest"></div> 
     <form>
        <input type="button" value="button" onClick="callTest()" />
     </form>
  </BODY>
</HTML>

main.js:

主要.js:

function change(p){
   window.glob = p;

   $('#divtest').html("<iframe id='IFRAMEtest' width='720' height='400' frameborder='0' src='init.html'></iframe>");
}

And in help.js :

在 help.js 中:

function callTest(){
 change('param');
}

When I click in button, displays "global variable", but I need to display "param".

当我点击按钮时,显示“全局变量”,但我需要显示“参数”。

In short, I need that a .js file read a global variable in another js file where this variable is fed into a function called by an event of a user.

简而言之,我需要一个 .js 文件读取另一个 js 文件中的全局变量,其中该变量被馈送到由用户事件调用的函数中。

Thanks.

谢谢。

edit- initialized global variable before importing files. js and using top. Works in IE and firefox, but chrome display "undefined"

编辑- 在导入文件之前初始化全局变量。js 并使用顶部。适用于 IE 和 firefox,但 chrome 显示“未定义”

回答by Rivasa

Take a look here: Global variables in Javascript across multiple filesThe main thing is, that you may have to declare the global variables before the actual file, so try inserting this before your inclusion to help.js

看看这里: 跨多个文件Javascript 中的全局变量主要是,您可能必须在实际文件之前声明全局变量,因此请尝试在包含到 help.js 之前插入它

so try giving this a shot.

所以试试看吧。

<script type='text/javascript' > 
  window.glob = "global variable"; 
</script>

so your code should be:

所以你的代码应该是:

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js" ></script>

    <script type='text/javascript' > 
        window.glob = "global variable";
    </script>

    <script type="text/javascript" charset="UTF-8" src="help.js" ></script>   
    <script type="text/javascript" charset="UTF-8" src="main.js" ></script>
</head>

try that and see if it works. also, remove your global variable declaration from main.js for this.

试试看,看看它是否有效。此外,为此从 main.js 中删除您的全局变量声明。

回答by Pointy

There's no global context that spans windows (frames), really. All frames in a "family" have access to a variable called "top" that refers to the topmost window, so you could use that.

真的没有跨窗口(框架)的全局上下文。“系列”中的所有框架都可以访问一个名为“top”的变量,该变量指的是最顶层的窗口,因此您可以使用它。

top.glob = "global variable";

and in your iframe code:

并在您的 iframe 代码中:

function test(){
  alert(top.glob);
}

editHere is a a slightly simplified version of your code, and it works fine for me.

编辑-这是您的代码的稍微简化版本,它对我来说很好用。

回答by Gabriel Gartz

When you are inside a frame and want to get a window variable from the parent window, you must refer to it.

当你在一个框架内并且想要从父窗口获取一个窗口变量时,你必须引用它。

Use topor window.top.

使用topwindow.top