Categories
JavaScript Answers

How to read the body of a Fetch promise with JavaScript?

Sometimes, we want to read the body of a Fetch promise with JavaScript.

In this article, we’ll look at how to read the body of a Fetch promise with JavaScript.

How to read the body of a Fetch promise with JavaScript?

To read the body of a Fetch promise with JavaScript, we can use the json method.

For instance, we write

const f = async () => {
  const response = await fetch("https://api.ipify.org?format=json");
  const data = await response.json();
  console.log(data);
};

to define function f that makes a get request with fetch.

Then we call response.json to return a promise with the response body object.

Finally, we use await to get the response body object and assign it to data.

Conclusion

To read the body of a Fetch promise with JavaScript, we can use the json method.

Categories
JavaScript Answers

How to get text box value in JavaScript?

Sometimes, we want to get text box value in JavaScript.

In this article, we’ll look at how to get text box value in JavaScript.

How to get text box value in JavaScript?

To get text box value in JavaScript, we can use the value property of the text box.

For instance, we write

<input type="text" name="txtJob" id="txtJob" />

to add an input element.

Then we write

const jobValue = document.getElementsByName("txtJob")[0].value;

to select the input boxes with name attribute txtJob with getElementsByName.

Then we use index 0 to get the first element.

And we get the value of the input with value.

Conclusion

To get text box value in JavaScript, we can use the value property of the text box.

Categories
JavaScript Answers

How to perform a DNS lookup from hostname to IP address using client-side JavaScript?

Sometimes, we want to perform a DNS lookup from hostname to IP address using client-side JavaScript.

In this article, we’ll look at how to perform a DNS lookup from hostname to IP address using client-side JavaScript.

How to perform a DNS lookup from hostname to IP address using client-side JavaScript?

To perform a DNS lookup from hostname to IP address using client-side JavaScript, we can use a 3rd party API.

For instance, we write

const response = await fetch("https://dns.google/resolve?name=example.com");
const json = await response.json();
console.log(json);

to call fetch in an async function to make a get request to https://dns.google/resolve with the name query parameter set to the hostname we want to resolve.

Then we get the IP address from the JSON response we get from response.json.

Conclusion

To perform a DNS lookup from hostname to IP address using client-side JavaScript, we can use a 3rd party API.

Categories
JavaScript Answers

How to check if iframe is loaded or it has a content with JavaScript?

Sometimes, we want to check if iframe is loaded or it has a content with JavaScript.

In this article, we’ll look at how to check if iframe is loaded or it has a content with JavaScript.

How to check if iframe is loaded or it has a content with JavaScript?

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe’s onload property to a function that runs when the iframe is loaded.

For instance, we write

document.querySelector("iframe").onload = () => {
  console.log("iframe loaded");
};

to select the iframe with querySelector.

Then we set its onload property to a function that runs when the iframe’s content is loaded.

Conclusion

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe’s onload property to a function that runs when the iframe is loaded.

Categories
JavaScript Answers

How to find the size of a file in Node.js?

Sometimes, we want to find the size of a file in Node.js.

In this article, we’ll look at how to find the size of a file in Node.js.

How to find the size of a file in Node.js?

To find the size of a file in Node.js, we can use the fs.statSync method.

For instance, we write

const fs = require("fs");
const stats = fs.statSync("myfile.txt");
const fileSizeInBytes = stats.size;
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);

to call the fs.statSync method with the path of the file we want to get the size for.

Then we get the size in bytes with stats.size.

Next, we convert the number of bytes to megabytes by dividing it by 1024 * 1024.

Conclusion

To find the size of a file in Node.js, we can use the fs.statSync method.