简体   繁体   English

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

[英]Javascript: Global variables shared between .js files

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: main.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". 当我点击按钮时,显示“全局变量”,但我需要显示“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. edit - 导入文件之前初始化的全局变量。 js and using top. js和使用顶部。 Works in IE and firefox, but chrome display "undefined" 适用于IE和Firefox,但Chrome显示“未定义”

Take a look here: Global variables in Javascript across multiple files The 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中的全局变量主要的是,您可能必须在实际文件之前声明全局变量,因此请在包含之前尝试插入这个变量来帮助.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中删除全局变量声明。

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. “family”中的所有帧都可以访问一个名为“top”的变量,该变量引用最顶层的窗口,因此您可以使用它。

top.glob = "global variable";

and in your iframe code: 并在您的iframe代码中:

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

editHere is aa slightly simplified version of your code , and it works fine for me. 编辑 - 这是您的代码的略微简化版本 ,它适用于我。

When you are inside a frame and want to get a window variable from the parent window, you must refer to it. 当您在框架内并希望从父窗口获取窗口变量时,您必须引用它。

Use top or window.top . 使用topwindow.top

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

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