Sometimes, we want to load local JSON file to show data in a HTML page using JavaScript.
In this article, we’ll look at how to load local JSON file to show data in a HTML page using JavaScript.
How to load local JSON file to show data in a HTML page using JavaScript?
To load local JSON file to show data in a HTML page using JavaScript, we define a global variable with the object.
For instance, we write
data = {
start: {
count: "5",
title: "start",
priorities: [
{
txt: "Work",
},
{
txt: "Time Sense",
},
{
txt: "Discipline",
}
],
},
};
in data.js to define the data
global variable set to an object.
Then we write
<html>
<head> </head>
<body>
<script src="data.js"></script>
</body>
</html>
to load it in our HTML code.
Conclusion
To load local JSON file to show data in a HTML page using JavaScript, we define a global variable with the object.