14

How can I call the variables that I stored in one javascript file from another?

var.js

var VAR = new Object;
VAR.myvalue = "Yeah!";

then I want to use VAR.myvalue here

sample.js

alert(VAR.myvalue);
Kevin
  • 53,822
  • 15
  • 101
  • 132
Robin Carlo Catacutan
  • 13,249
  • 11
  • 52
  • 85

3 Answers3

12

First, instead of

var VAR = new Object;
VAR.myvalue = "Yeah!";

Opt for

var VAR = {
    myvalue: "Yeah!"
 };

But so long as var.js is referenced first, before sample.js, what you have should work fine.

var.js will declare, and initialize VAR, which will be read from the script declared in sample.js

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • I created a sample just for now, yeah it works thanks. But in my case because I'm applying this on a widget and it didn't. Maybe there's problem with my code. :) but, thanks. – Robin Carlo Catacutan Dec 02 '11 at 03:26
2

Include both JavaScript file in one HTML file, place sample.js after var.js so that VAR.myvalue is valid:

<script type="text/javascript" src="var.js"></script>
<script type="text/javascript" src="sample.js"></script>
ExpExc
  • 3,828
  • 1
  • 15
  • 20
2

Try separating your scope using a module pattern. This will eliminate headaches in the future.

var.js

var someVar = (function () {
  var total = 10; // Local scope, protected from global namespace

  return {
    add: function(num){
      total += num;
    }
  , sub: function(num){
      total -= num;
    }
  , total: function(){
      return total;
    }
  };
}());

Then you can use that object's methods and properties from anywhere else.

sample.js

someVar.add(5);
someVar.sub(6);
alert(someVar.total());
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
  • Hey, this is one works on my widget. Can you make this more simple because I just want to store my values to other javascript and just call it. Just what I've stated above. Thanks for advance :) – Robin Carlo Catacutan Dec 02 '11 at 03:58
  • LOL, the point was to 'learn you something' about Javascript OOP. Adam Rackis answer above is the same without the methods, only object properties but essentially works the same way. – Pastor Bones Dec 02 '11 at 04:02
  • @Pastor Bones - you didn't actually answer the question: the key point that you didn't mention is that "var.js" must be included _before_ "sample.js". (Also, I agree that the module pattern can be useful, but only if you actually need methods. If you just need a few data properties a simple object literal is the way to go.) – nnnnnn Dec 02 '11 at 04:12
  • @Pastor, yeah but what I've stated above it didn't work on my widget. I just want to ask if there's better method in doing that, that will save more space. Because there's a lot of values I want to store. – Robin Carlo Catacutan Dec 02 '11 at 04:27
  • the return statement can return functions or properties and both are accessed in the same way. – Pastor Bones Dec 02 '11 at 04:39
  • I know that, but anything more than an object literal is overkill if you know you don't need functions. – nnnnnn Dec 02 '11 at 04:43