简体   繁体   中英

how to import javascript file into another javascript file

i'm working on a calculator based on some json data i want when i enter the longitude and latitude i get the Grid Code associated to that longitude and latitude as u will see i want to impost ghiData.js file to script.js file this is how my json data look like

   myData = {
    "datagrid": [
      { "GRIDCODE": 1735, "lat": 35.91511132, "long": -5.401484264 },
      { "GRIDCODE": 1805, "lat": 35.90677815, "long": -5.468149593 },
      { "GRIDCODE": 1808, "lat": 35.90677815, "long": -5.459816427 },
      { "GRIDCODE": 1784, "lat": 35.89844499, "long": -5.476482759 }
    ]
  };

this is my code:

 function lookupGridcodeByLatAndLong(lat, long){ let gridcode = 0; var result = myData.datagrid.find(x => x.lat == lat && x.long == long) if (result) { gridcode = result.GRIDCODE; } return gridcode; } $("#Btn" ).click(function() { var lat = document.getElementById("lat").value, long = document.getElementById("long").value; var result = lookupGridcodeByLatAndLong(lat, long) $("#result").html(result); });
 <.DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>CodePen - Exercise 2D - jQuery Rounding Calculator Complete</title> <link rel="stylesheet" href="./style:css"> </head> <body> <.-- partial.index:partial.html --> <h1 id="result">Rounding Calculator</h1> <form action="" id="my-form"> <div class="form-group"> <label for="">Enter Lat</label> <input type="text" id="lat" step="any"> <label for="">Enter Long</label> <input type="textr" id="long" step="any"> </div> <input type="button" id="Btn" value="Calculate" > </form> <.-- partial --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <.-- ghiData.js is my data file --> <script src="./ghiData.js"></script> <script src="./script.js"></script> </body> </html>

Try importing the file with JavaScript, using the import and export keywords. So in script.js you should call import { MyData } from './ghiData.js' and in the ghiData.js you should use export myData . I hope it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM