Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

90
Views
how to import variable from javasvript to html script tag

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.

enter image description here

So I want to detach that variable to another file(maybe .js file? any format no matter.)

  1. Where I should save that variable? is it .js file?
  2. How can I import that variable in HTML script tag?

Thanks for your help!

5 months ago · Juan Pablo Isaza
2 answers
Answer question

0

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

5 months ago · Juan Pablo Isaza Report

0

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();
5 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs