I have two JS files where I would like to access the variable associated with my JS Object.
rates.js
var CAD = {
USD : 1.2727,
EUR : 1.54,
CNY : 0.20,
BTC : 45139.58
CZK : 0.059,
GBP : 1.7338,
CHF : 1.4345,
JPY : 0.0123,
AUD : 0.9827,
PLN : 0.3405,
ZAR : 0.0839
}
var CHF = {
USD : 0.8911,
EUR : 1.0766,
CNY : 0.14,
BTC : 32154.03,
CZK : 0.041,
GBP : 1.2086,
CHF : 1,
JPY : 0.0086,
AUD : 0.685,
PLN : 0.2375,
ZAR : 0.0584
}
Second file where I would like to store a value in a variable:
dropdown.js
if(getTicker == "AUDCAD" ){
var price = CAD.USD;
alert(price);
}
I have used the following lines in my html file in an effort to connect the two with no success.
<script src="rates.js"></script>
<script src="dropdown.js"></script>
</body>
</html>
I am not using a server therefore I cannot make modules to import and export. Are there any other recommendations?
window.CAD = {...And using it indropdown.jsas follows:var price = window.CAD.USD;window, but it is discouraged. Because because they can be accessed from ANY function and you risk having multiple functions trying to read from and write to the same variables. Please see the modern approach I mention in my answer. Although using global variable withwindowis a common practice(unfortunately).