Categories
JavaScript Answers

How to access JPEG EXIF rotation data in JavaScript on the client side?

To access JPEG EXIF rotation data in JavaScript on the client side, we can use the exif-js library.

To add it, we run

<script src="https://cdn.jsdelivr.net/npm/exif-js"></script>

Then we use it by writing

const img1 = document.getElementById("img1");
EXIF.getData(img1, function () {
  const make = EXIF.getTag(this, "Make");
  const model = EXIF.getTag(this, "Model");
  const makeAndModel = document.getElementById("makeAndModel");
  makeAndModel.innerHTML = `${make} ${model}`;
});

to get the img element with getElementById.

Then we call getData with the img element and a callback that gets the exif data.

We call EXIF.getTag to get the exif tag we’rew looking for.

Categories
JavaScript Answers

How to reset (clear) form through JavaScript?

Sometimes, we want to reset (clear) form through JavaScript.

In this article, we’ll look at how to reset (clear) form through JavaScript.

How to reset (clear) form through JavaScript?

To reset (clear) form through JavaScript, we call the form element’s reset method.

For instance, we write

document.forms["frm_id"].reset();

to get the form with ID frm_id with document.forms["frm_id"].

Then we call reset to clear the values in the form.

Conclusion

To reset (clear) form through JavaScript, we call the form element’s reset method.

Categories
JavaScript Answers

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

Sometimes, we want to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

In this article, we’ll look at how to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.

For instance, we write

const res = await fetch("yourfile.json");
const data = await res.clone().json();

to call res.clone before we call json to clone the response object before we return a promise with the JSON response body.

Conclusion

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.

Categories
JavaScript Answers

How to convert JSON to CSV format and store in a variable with JavaScript?

Sometimes, we want to convert JSON to CSV format and store in a variable with JavaScript.

In this article, we’ll look at how to convert JSON to CSV format and store in a variable with JavaScript.

How to convert JSON to CSV format and store in a variable with JavaScript?

To convert JSON to CSV format and store in a variable with JavaScript, we can use some array methods.

For instance, we write

const replacer = (key, value) => (value === null ? "" : value);
const header = Object.keys(items[0]);
const csv = [
  header.join(","),
  ...items.map((row) =>
    header
      .map((fieldName) => JSON.stringify(row[fieldName], replacer))
      .join(",")
  ),
].join("\r\n");

console.log(csv);

to get the header arrat with Object.keys called on the first entry in the items array.

Then we create the csv array by creating the header row with header.join(",").

Then we call items.map to map the rest of the entries in items to csv row strings.

And then we join the row strings with '\r\n' with join.

Conclusion

To convert JSON to CSV format and store in a variable with JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to run JavaScript when an element loses focus?

Sometimes, we want to run JavaScript when an element loses focus.

In this article, we’ll look at how to run JavaScript when an element loses focus.

How to run JavaScript when an element loses focus?

To run JavaScript when an element loses focus, we can listen for the blur event.

For instance, we write

<input type="text" name="name" value="value" />

to add an input.

Then we write

const input = document.querySelector("input");
input.onblur = () => {
  alert(1);
};

to select the input with querySelector.

And then we set input.onblur to a function that calls alert to show an alert box when we move our focus away from the input.

Conclusion

To run JavaScript when an element loses focus, we can listen for the blur event.