Categories
JavaScript Answers

How to insert HTML into a div with JavaScript?

To insert HTML into a div with JavaScript, we can set the innerHTML property.

For instance, we write

document.getElementById("tag-id").innerHTML = "<ol><li>html data</li></ol>";

to select the div with getElementById.

Then we set its innerHTML property to a string with the HTML we want to render.

Categories
JavaScript Answers

How to get visitor’s location using geolocation with JavaScript?

To get visitor’s location using geolocation with JavaScript, we can use the Ipregistry API.

For instance, we write

const response = await fetch("https://api.ipregistry.co/?key=tryout");
const payload = await response.json();
console.log(payload.location.country.name, payload.location.city);

to call fetch to make a get request to https://api.ipregistry.co/?key=tryout

Then we get the response from it with json.

And then we get the country and city data from the payload.

We put that in an async function.

Categories
JavaScript Answers

How to create a function in JavaScript that can be called only once?

To create a function in JavaScript that can be called only once, we can set a property on the function after it’s executed.

For instance, we write

const myFunc = () => {
  if (myFunc.fired) {
    return;
  }
  myFunc.fired = true;
  //...
};

to check the myFunc.fired property is set to true.

If it is, then we stop running myFunc with return.

Otherwise, we set myFunc.fired to true and run the rest of the code.

Conclusion

To create a function in JavaScript that can be called only once, we can set a property on the function after it’s executed.

Categories
JavaScript Answers

How to get progress from XMLHttpRequest with JavaScript?

To get progress from XMLHttpRequest with JavaScript, we can set the onprogress property to a function that gets the request’s progress.

For instance, we write

const progressBar = document.getElementById("p");
const client = new XMLHttpRequest();
client.open("GET", "/foo/bar");
client.onprogress = (pe) => {
  if (pe.lengthComputable) {
    progressBar.max = pe.total;
    progressBar.value = pe.loaded;
  }
};
client.onloadend = (pe) => {
  progressBar.value = pe.loaded;
};
client.send();

to create the XMLHttpRequest client.

Then we call open to make a get request to /foo/bar.

We then set client.onprogress to a function that gets the progress from toe pe.total and pe.loaded properties.

loaded has the amount of data in bytes that arrived and total has the total number of bytes that we’re supposed to get.

Then we call client.send to make the request.

Conclusion

To get progress from XMLHttpRequest with JavaScript, we can set the onprogress property to a function that gets the request’s progress.

Categories
JavaScript Answers

How to call map() on an iterator with JavaScript?

To call map() on an iterator with JavaScript, we convert the iterator into an array.

For instance, we write

Array.from(m).map(([key, value]) => {
  //...
});

to call Array.from with iterator m to convert it to an array.

Then we get the key and value from the array parameter in the map callback and do what we want with the values.

Conclusion

To call map() on an iterator with JavaScript, we convert the iterator into an array.