Categories
JavaScript Answers

How to load local JSON file to show data in a HTML page using JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.