Categories
JavaScript Answers

How to retrieve HTML5 video duration separately from the file with JavaScript?

Sometimes, we want to retrieve HTML5 video duration separately from the file with JavaScript.

In this article, we’ll look at how to retrieve HTML5 video duration separately from the file with JavaScript.

How to retrieve HTML5 video duration separately from the file with JavaScript?

To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange event.

For instance, we write

const myVideoPlayer = document.getElementById("video_player");

myVideoPlayer.addEventListener("durationchange", () => {
  console.log("Duration change", myVideoPlayer.duration);
});

to select the video element with getElementById.

Then we listen for the durationchange event with addEventListener.

And we get the duration of the video with the duration property.

Conclusion

To retrieve HTML5 video duration separately from the file with JavaScript, we listen for the durationchange event.

Categories
JavaScript Answers

How to convert a 2D JavaScript array to a 1D array?

Sometimes, we want to convert a 2D JavaScript array to a 1D array.

In this article, we’ll look at how to convert a 2D JavaScript array to a 1D array.

How to convert a 2D JavaScript array to a 1D array?

To convert a 2D JavaScript array to a 1D array, we use the array flat method.

For instance, we write

const arrToConvert = [
  [0, 0, 1],
  [2, 3, 3],
  [4, 4, 5],
];
const newArr = arrToConvert.flat();

to call arrToConvert.flat to return a flattened version of the arrToConvert array.

Conclusion

To convert a 2D JavaScript array to a 1D array, we use the array flat method.

Categories
JavaScript Answers

How to stop a YouTube video with JavaScript?

Sometimes, we want to stop a YouTube video with JavaScript.

In this article, we’ll look at how to stop a YouTube video with JavaScript.

How to stop a YouTube video with JavaScript?

To stop a YouTube video with JavaScript, we call the stopVideo method.

For instance, we write

player.stopVideo();

to call the YouTube player‘s stopVideo method to stop the video.

Conclusion

To stop a YouTube video with JavaScript, we call the stopVideo method.

Categories
JavaScript Answers

How to convert month name to month number in JavaScript?

Sometimes, we want to convert month name to month number in JavaScript.

In this article, we’ll look at how to convert month name to month number in JavaScript.

How to convert month name to month number in JavaScript?

To convert month name to month number in JavaScript, we use the date toLocaleDateString method.

For instance, we write

const month = new Date().toLocaleDateString(`en`, { month: `2-digit` });

to call toLocaleDateString with the locale string and an object with the month property.

We set it to '2-digit' to return a 2 digit month.

Conclusion

To convert month name to month number in JavaScript, we use the date toLocaleDateString method.

Categories
React Answers

How to fix ReferenceError: document is not defined with Next.js and JavaScript?

Sometimes, we want to fix ReferenceError: document is not defined with Next.js and JavaScript.

In this article, we’ll look at how to fix ReferenceError: document is not defined with Next.js and JavaScript.

How to fix ReferenceError: document is not defined with Next.js and JavaScript?

To fix ReferenceError: document is not defined with Next.js and JavaScript, we check if document is defined.

For instance, we write

if (typeof document === "undefined") {
  // ...
} else {
  // ...
}

in our component to check if document is undefined with typeof document === "undefined".

If it’s not defined, then we render items on server side.

Otherwise, we render client side components.