html-examples
html-examples copied to clipboard
preload JSON
Hello, do you have any examples of preloading JSON
Hi there @suyizhang thanks for asking.
You should be able to do something like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Load JSON Example</title>
<link href="style.css" rel="stylesheet" />
<link rel="preload" href="data.json" as="fetch" />
</head>
<body>
<h1>Loading JSON Example</h1>
<script>
async function loadData() {
try {
const res = await fetch("data.json");
const data = await res.json();
console.log(data);
} catch (err) {
console.error(err);
}
}
loadData();
</script>
</body>
</html>
Which assumes that you have these resources:
├── data.json
├── index.html
└── style.css
Does that answer the question?
Closing for the moment as I think the question is answered