tags:

views:

47

answers:

3

hello folks., is it possible to use output of first.js file as input to another second.js file. first.js contains some colorcodes.i need to use this colorcodes into my second.js file. any pointers?

+3  A: 

If you store your colorcodes in a global variable you should be able to access it from either javascript file.

Fermin
+3  A: 

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> 
no
In a browser, `window` *is* the global scope - so window.colorCodes and the (global) object colorCodes is the same object.
Piskvor
True... the reason I mention it is for cases where you need to set a global variable from a non-global scope.
no
A: 

This should work - define a global variable in firstfile and access it from secondfile:

<script src="/firstfile.js"></script>
<script src="/secondfile.js"></script>

firstfile.js:

var colors = {
   text:'#000000',
   background:'#aaaaaa',
   something_else:'blue'
};

secondfile.js:

do_something_with(colors.background);

Note that the order in which you load the script files is significant for some browsers (IE6 for sure, maybe others)

Piskvor