I'm making webpage with django templete. In the HTML script tag, I make a variable which contains geojson data, so that it is too long to be contained in a single file.(about 10000 lines) So I want to detach it another place. var "areas" in the image is it.
So I want to detach that variable to another file(maybe .js file? any format no matter.)
Thanks for your help!
Just Use A JS File with just the variable and importing it through script tag would work fine. Like if it was saved in areas.js
use the code
<script src = "areas.js"></script>
in the body tag then you can just the use variable like normal in scripts
You can put that data in a .json
file on your server.
Than do something like following to fetch the json and parse it.
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => {
// Use data in your app.
});
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
You can also use async/await to avoid the .then and callback.
async function fetchMoviesJSON() {
const response = await fetch('/movies.json');
const movies = await response.json();
// Use the data in your app.
}
fetchMoviesJSON();