I have an app and different javascript files.
From HTML I want to be able to access var from those different javascript files.
How should I do that??
EXAMPLE JAVASCRIPT:
var one = {
att1: 'myString1',
att2: 'myString2'
...
};
var two = {
att1: 'myString3',
att2: 'myString4',
...
};
EXAMPLE HTML:
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" charset="utf-8" src="js/idiomas.js"></script>
<script type="text/javascript" src="jqueryMobile/jquery-1.11.0.js"/></script>
......
EDIT:
Sorry, missed one part. This is where I try to access the data:
<script type="text/javascript">
var atrib1 = document.getElementById("idAtr1");
var atrib4 = document.getElementById("idAtr4");
.....
atrib1.innerHTML = one.att1;
atrib4.innerHTML = two.att2;
....
</script>
You have to maintain hierarchy. For example.
<script src="file1.js"></script>
<script src="file2.js"></script>
then you can able to access file1.js variables on file2.js but can't access file2.js on file1.js. Rules is javascript read files sequentially. So, if you try to access function/variables/object before they are available will get error.
Otherwise, If you want to access both files variable, you may try followings:
<script src="file1.js"></script>
<script src="file2.js"></script>
<!-- import your js files that contain those variable -->
<script>
var atrib1 = document.getElementById("idAtr1");
var atrib4 = document.getElementById("idAtr4");
//import your js files above and then call it.
atrib1.innerHTML = one.att1;
atrib4.innerHTML = two.att2;
</script>
If you have still doubt. Mention that on comment. We'll try to help :)
Simply add those js files in your HTML, and access those variables normally.
<!-- Your script containing the variables -->
<script type="text/javascript" charset="utf-8" src="js/idiomas.js"></script>
...
<script type="text/javascript">
console.log(one); // Access the variable normally
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.