title: Javascript: Global variables shared between .js files
body: <p>I'm having problems with global variables.</p>
  
  <p>Considering that I have the following files: init.html, main.html, init.js, main.js and help.js :</p>
  
  <p>Where, init.html:</p>
  
  <pre><code>&lt;HTML&gt;
     &lt;HEAD&gt;
     &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"&gt;&lt;/script&gt;
     &lt;script type="text/javascript" charset="UTF-8" src="init.js" &gt;&lt;/script&gt;
     &lt;script type="text/javascript" charset="UTF-8" src="main.js" &gt;&lt;/script&gt;
     &lt;script type="text/javascript" charset="UTF-8" src="help.js" &gt;&lt;/script&gt;
  
     &lt;/HEAD&gt;
     &lt;BODY&gt;
         &lt;script&gt;
            $(document).ready(function() {
            test();
          });
         &lt;/script&gt;
    &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>In init.js :</p>
  
  <pre><code>function test(){
  alert(window.glob);
  }
  </code></pre>
  
  <p>In main.html :</p>
  
  <pre><code>&lt;HTML&gt;
    &lt;HEAD&gt;
        &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"&gt; &lt;/script&gt;
        &lt;script type='text/javascript' &gt; 
            top.glob = "global variable";
        &lt;/script&gt;
        &lt;script type="text/javascript" charset="UTF-8" src="help.js" &gt;&lt;/script&gt;   
        &lt;script type="text/javascript" charset="UTF-8" src="main.js" &gt;&lt;/script&gt;
    &lt;/HEAD&gt;
    &lt;BODY&gt;    
       &lt;div id="divtest"&gt;&lt;/div&gt; 
       &lt;form&gt;
          &lt;input type="button" value="button" onClick="callTest()" /&gt;
       &lt;/form&gt;
    &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>main.js: </p>
  
  <pre><code>function change(p){
     window.glob = p;
  
     $('#divtest').html("&lt;iframe id='IFRAMEtest' width='720' height='400' frameborder='0' src='init.html'&gt;&lt;/iframe&gt;");
  }
  </code></pre>
  
  <p>And in help.js :</p>
  
  <pre><code>function callTest(){
   change('param');
  }
  </code></pre>
  
  <p>When I click in button, displays "global variable", but I need to display "param".</p>
  
  <p>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.</p>
  
  <p>Thanks.</p>
  
  <p><em><strong>edit</em> - initialized global variable before importing files. js and using top. Works in IE and firefox, but chrome display "undefined"</strong></p>