Skip to main content

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

// first.js var colorCodes = { back : "#fff", front : "#888", side : "#369" }; 
// first.js var colorCodes = { back : "#fff", front : "#888", side : "#369" }; 

... in another file ...

// second.js alert (colorCodes.back); // alerts `#fff` 
// second.js alert(colorCodes.back); // alerts `#fff` 

... in your html file ...

<script type="text/javascript" src="first.js"></script> <script type="text/javascript" src="second.js"></script> 
<script type="text/javascript" src="first.js"></script> <script type="text/javascript" src="second.js"></script> 

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

// first.js var colorCodes = { back : "#fff", front : "#888", side : "#369" }; 

... in another file ...

// second.js alert (colorCodes.back); // alerts `#fff` 

... in your html file ...

<script type="text/javascript" src="first.js"></script> <script type="text/javascript" src="second.js"></script> 

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

// first.js var colorCodes = { back : "#fff", front : "#888", side : "#369" }; 

... in another file ...

// second.js alert(colorCodes.back); // alerts `#fff` 

... in your html file ...

<script type="text/javascript" src="first.js"></script> <script type="text/javascript" src="second.js"></script> 
Source Link
Dagg Nabbit
  • 77k
  • 19
  • 115
  • 142

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

// first.js var colorCodes = { back : "#fff", front : "#888", side : "#369" }; 

... in another file ...

// second.js alert (colorCodes.back); // alerts `#fff` 

... in your html file ...

<script type="text/javascript" src="first.js"></script> <script type="text/javascript" src="second.js"></script>