Start HTML5 video from a particular time in JavaScript

Using video tags is the easiest way to add a video player to the web page to play video. You can do a lot with this video player with HTML5 DOM JavaScript. In this tutorial, I am going to show you how to start an HTML5 video from a particular time in JavaScript.

The task is very simple. Here you just have to use one single line of JavaScript code to play the HTML video from any time duration.

Let’s first create our HRML5 video player:

<video id="myVideo" width="640" height="480" controls>
  <source src="jn.mp4" type="video/mp4">
</video>

We have set the ID of our video player as “myVideo”.

Now let’s, see the JavaScript code below:

document.getElementById('myVideo').currentTime = 15; // Set the starting time to 15 seconds

In the above line of JavaScript code, we have set the currentTime property of our HTML video to 15 seconds. The currentTime property will set the starting time of the video. Here I have set it to 15 seconds.

That’s it.

Now it is ready. You can run it on your browser and you can see the video starts from 15 seconds instead of starting from the beginning.

You can set any time duration to start the HTML video as you want with the JavaScript code.

2 responses to “Start HTML5 video from a particular time in JavaScript”

  1. Hanna says:

    No, that’s not it.

    can’t access property “currentTime”, document.getElementById(…) is null

Leave a Reply

Your email address will not be published. Required fields are marked *