Home > database >  Setting up a JS file to get its function triggered
Setting up a JS file to get its function triggered

Time:12-29

I’m learning JavaScript and in a few scripts that I have come across I can see a reference to what seems like apis and then functions. For example:

      // Call the what3words
      what3words.api
        .gridSectionGeoJson({
          southwest: {
            lat: sw.lat,
            lng: sw.lng
          },
          northeast: {
            lat: ne.lat,
            lng: ne.lng
          }
        })

I’ve tried to replicate this by swapping this code out for my own version by doing the following:

  1. Remove what3words.js
  2. Include my own script file script.js in the headers for that page
  3. Include a function inside that file, such as this:

function gridSectionGeoJson() {
  document.getElementById("demo").innerHTML = "Hello World!";
}

However this never gets triggered. How do I setup my script.js so that my function gets triggered by the code in the top snippet? I imagine that has to do with the reference to “what3words.api” that I need to somehow designate inside my own JS file to get it to trigger.

CodePudding user response:

In the script block above you would just call gridSectionGeoJson(); given the script you have above. You typically want to include all scripts at the end of the page when possible.

  • Related