Reputation: 671
How can I use a variable of one in another in javascript?
Upvotes: 14
Views: 19280
Reputation: 14243
When you declared a variable inside a <script>
, you can access it anywhere simply by using this:
var myValue= window.yourProperty;
Upvotes: 0
Reputation: 61
If the two script tags are on the same page, any variable declared outside of the scope of a function is global to the page. However, it does matter which script is loaded first for some applications.
Upvotes: 6
Reputation: 37267
Make it global;
script1.js
var foobar = "O HAI";
script2.js
alert(foobar);
Upvotes: 10
Reputation: 19870
If the two script tags are on the same page, any variable declared outside of the scope of a function is global to the page.
Upvotes: 17