Categories
JavaScript Answers

How to ignore user’s time zone and force Date() use specific time zone with JavaScript?

To ignore user’s time zone and force Date() use specific time zone with JavaScript, we can use the moment timezone library.

To install it, we run

npm install moment-timezone --save

Then we write

const moment = require("moment-timezone");
const d = moment(1270544790922)
  .tz("Europe/London")
  .format("YYYY-MM-DD HH:mm:ss");

to call moment with a timestamp in milliseconds.

And we set the time zone of the datetime with the tz method.

Then we call format to return a datetime string with the value returned by tz.

Categories
JavaScript Answers

How to add event handler with parameters with JavaScript?

Sometimes, we want to add event handler with parameters with JavaScript.

In this article, we’ll look at how to add event handler with parameters with JavaScript.

How to add event handler with parameters with JavaScript?

To add event handler with parameters with JavaScript, we can create a function that takes the parameters we want.

For instance, we write

const addClickHandler = (elem, arg1, arg2) => {
  elem.addEventListener(
    "click",
    (e) => {
      // ...
    },
    false
  );
};

to define the addClickHandler function that takes a few parameters.

In it, we call elem.addEventListener to add a click event listener to the elem element.

We can do whatever we want with arg1 and arg2 in the click listener callback.

Conclusion

To add event handler with parameters with JavaScript, we can create a function that takes the parameters we want.

Categories
JavaScript Answers

How to determine if a div is scrolled to the bottom with JavaScript?

Sometimes, we want to determine if a div is scrolled to the bottom with JavaScript.

In this article, we’ll look at how to determine if a div is scrolled to the bottom with JavaScript.

How to determine if a div is scrolled to the bottom with JavaScript?

To determine if a div is scrolled to the bottom with JavaScript, we check if scrollTop is the same as scrollHeight - offsetHeight.

For instance, we write

if (obj.scrollTop === obj.scrollHeight - obj.offsetHeight) {
}

to check if obj.scrollTop is equal to obj.scrollHeight - obj.offsetHeight.

If it is, then we scrolled to the bottom of the obj element.

Conclusion

To determine if a div is scrolled to the bottom with JavaScript, we check if scrollTop is the same as scrollHeight - offsetHeight.

Categories
JavaScript Answers

How to load an image to a img element from a file input with JavaScript?

Sometimes, we want to load an image to a img element from a file input with JavaScript.

In this article, we’ll look at how to load an image to a img element from a file input with JavaScript.

How to load an image to a img element from a file input with JavaScript?

To load an image to a img element from a file input with JavaScript, we can use the FileReader constructor.

For instance, we write

<input type="file" />
<img id="myimage" height="200" />

to add the file input and the img element.

Then we write

const input = document.querySelector("input");

input.onchange = (event) => {
  const selectedFile = event.target.files[0];
  const reader = new FileReader();

  const imgtag = document.getElementById("myimage");
  imgtag.title = selectedFile.name;

  reader.onload = (event) => {
    imgtag.src = event.target.result;
  };

  reader.readAsDataURL(selectedFile);
};

to select the file input with querySelector.

And then we set its onchange property to a function that runs when we change file selection.

We get the first selected file with event.target.files[0].

Then we create a FileReader object and call readAsDataURL on it with the selectedFile as its argument.

When the file is read, read.onload runs.

In it, we set the imgtag.src property to event.target.result, which the base64 URL string with the image data.

We set imgtag to the img element that we selected with getElementById.

Conclusion

To load an image to a img element from a file input with JavaScript, we can use the FileReader constructor.

Categories
JavaScript Answers

How to convert ISO date to milliseconds in JavaScript?

Sometimes, we want to convert ISO date to milliseconds in JavaScript.

In this article, we’ll look at how to convert ISO date to milliseconds in JavaScript.

How to convert ISO date to milliseconds in JavaScript?

To convert ISO date to milliseconds in JavaScript, we can use the date’s getTime method.

For instance, we write

const date = new Date("11/21/1987 16:00:00");
const milliseconds = date.getTime();
console.log(milliseconds);

to create a Date object.

Then we call getTime on it to get the timestamp in milliseconds.

Conclusion

To convert ISO date to milliseconds in JavaScript, we can use the date’s getTime method.