Javascript:全局变量在.js文件之间共享 [英] Javascript: Global variables shared between .js files

查看:2803
本文介绍了Javascript:全局变量在.js文件之间共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



其中,init.html:

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

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

在init.js中:

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

在main.html中:

 < HTML> 
< HEAD>
< script type =text / javascriptsrc =http://code.jquery.com/jquery-1.6.3.min.js> < /脚本>
< script type ='text / javascript'>
top.glob =全局变量;
< / script>
< script type =text / javascriptcharset =UTF-8src =help.js>< / script>
< script type =text / javascriptcharset =UTF-8src =main.js>< / script>
< / HEAD>
< BODY>
< div id =divtest>< / div>
< form>
< input type =buttonvalue =buttononClick =callTest()/>
< / form>
< / BODY>
< / HTML>

main.js:

 函数变更(p){
window.glob = p;

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

在help.js中:

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

当我点击按钮时,显示全局变量,但我需要显示param。

总之,我需要一个.js文件读取另一个js文件中的全局变量,其中此变量被馈入由事件调用的函数。

感谢。



编辑 - 初始化的全局在导入文件之前变量。 JS和使用顶部。在IE和firefox中工作,但chrome显示未定义

解决方案

请看这里:
跨多个文件的Javascript中的全局变量
主要的东西是的,你可能需要在实际文件之前声明全局变量
,所以试着在你包含到help.js之前插入这个全局变量



所以试着给这个一个镜头。

 < script type ='text / javascript'> 
window.glob =全局变量;
< / script>

所以你的代码应该是:

 < HEAD> 
< script type =text / javascriptsrc =http://code.jquery.com/jquery-1.6.3.min.js>< / script>

< script type ='text / javascript'>
window.glob =全局变量;
< / script>

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

试试看看它是否有效。
也是这样的,从main.js中移除你的全局变量声明。


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 :

Where, 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 :

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

In 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:

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 :

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.

Thanks.

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

解决方案

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

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.

这篇关于Javascript:全局变量在.js文件之间共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆