I have 2 js files: 1.js and 2.js.
In 1.js I have a variable
var test ='Hello';
I'm trying to access the variable in 2.js
alert(test);
Sample Code:
1.js :
$(document).ready(function () {
var test = 'Hello';
});
2.js :
$(function () {
function getData(){
alert(test);
}
});
In my aspx page order of js files:
<script type="text/javascript" src="js/1.js"></script>
<script type="text/javascript" src="js/2.js?"></script>
I know the question is asked before many times but none of the answers seems to work for me.
testis scoped to the function it’s defined in, here the anonymous function passed in to jQuery’s DOMReady shortcut.