Creating dynamic visual elements on a webpage using HTML: Video

As I continue to learn HTML, CSS, and JavaScript to create a basic website for educational purposes, I have successfully embedded a video stored in my drive using the video element on an HTML page. However, I now aim to also display the video's title, total duration, and view count alongside the video itself. Can someone guide me on where to focus more of my efforts and provide resources for learning how to achieve this integration?

Here is my current HTML code snippet:

* {
 padding:0px;
 margin:0px;
}

/* Rest of the CSS styles go here */

<!DOCTYPE HTML>
<html lang = "en">
  <head>
    <title>Videos</title>
      <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="videos.css">
  </head>

  <body>
    <header>
      <nav>
        <h1> Main logo </h1>
          <ul id="nav">
            <li><a class="homeblack" href="index.html">Home</a></li>
            <li><a class="homered" href="videos.html">Videos</a></li>
          </ul>
      </nav>
    </header>
    <div class="divider"></div>
    <div id="navBar">
      <ul>
        <li><a href="#">Education Videos</a></li>
        <li><a href="#">Film Videos</a></li>
        <li><a href="#">Other Videos</a></li>
      </ul>
    </div>
    
    <video title="Video Title" controls width="250" height="160"> 
      <source src="videoplayback.mp4" />
    </video> 

    <video title="Another Video Title" controls width="250" height="160"> 
      <source src="videoplayback (1).mp4" />
    </video> 
    
    <video title="One More Video Title" controls width="250" height="160"> 
      <source src="1 Ton.mp4" /> 
    </video> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  
  </body>
</html>

Answer №1

Tracking views on the client-side using HTML, CSS, and Javascript is not feasible because once a user navigates away from the page, that data is lost. To accurately track views, you will need to implement a backend system such as a database or logging file.

Additionally, native HTML5 video tags do not include metadata like the length of the video. However, this information can be added to the markup in other ways.

You may find more information on displaying views on HTML5 videos by visiting this link: How to display views on html5 video?

Answer №2

Desired Information:

...to showcase the video's title, total duration, and overall views...

  1. display the video's title
  2. length of the video
  3. total number of views

1:

If you're referring to the video's file name as the "title of video," here's a solution:

Array.from(document.querySelectorAll("video")).forEach(video=>{
    const title = (video.src || video.children[0].src).replace(/.*\//g, "").replace(/\.[a-zA-Z0-9_-]+/, "").replace(/-|_/g, " ");
    video.insertAdjacentHTML("beforeBegin", title);
});

2:

Array.from(document.querySelectorAll("video")).forEach(video=>video.addEventListener('loadedmetadata', function() {
    const duration = videoPlayer.duration;
    video.insertAdjacentHTML("afterEnd", Math.floor(video.duration / (60 * 60)) + ":" + Math.floor(video.duration / 60) + ":" + video.duration % 60)
}));

3:

Refer to this answer.

DEMO:

Array.from(document.querySelectorAll("video")).forEach(video=>{
    const title = (video.src || video.children[0].src).replace(/.*\//g, "").replace(/\.[a-zA-Z0-9_-]+/, "").replace(/-|_/g, " ");
    video.insertAdjacentHTML("beforeBegin", title);
});
Array.from(document.querySelectorAll("video")).forEach(video=>video.addEventListener('loadedmetadata', function() {
    const duration = videoPlayer.duration;
    video.insertAdjacentHTML("afterEnd", Math.floor(video.duration / (60 * 60)) + ":" + Math.floor(video.duration / 60) + ":" + video.duration % 60)
}));
* {
 padding:0px;
 margin:0px;
}


body{
  margin: 0;
 }

header {
  background: green;
  color: white;
  padding: 8px 0px 6px 40px;
  height: 50px;
}

header h1 {
 display: inline;
 font-family: 'Oswald',sans-serif;
 font-weight: 400;
 font-size: 32px;
 float: left;
 margin-top: 0px;
 margin-right: 10px;
}

nav ul{
  display: inline;
 padding: 0px;
float: left;
}

nav ul li{
 display: inline-block;
 list-style-type:none;
 color: white;
 float: left;
 margin-left: 15px;
}

nav ul li a{
 color: white;
 text-decoration: none;
}

#nav {
 font-family: 'Montserrat', sans-serif;
}

.homered{
 background-color: red;
 padding: 30px 10px 25px 10px;

}

.divider{
 background-color: red;
 height: 5px;

}

.homeblack:hover{
 background-color: blue;
 padding: 30px 10px 25px 10px;
}
div{
 width:250px;
 height:666px;
 background:#1A8DA9;
}
div a{
 text-decoration:none;
 color:white;
 padding:20px;
 padding-right:100px
}
div ul li{
 list-style-type:none;
 display:block;
 padding :15px;

 border-bottom:1px solid #236D7F;
 }
 div ul li :hover{
  background:#4097AD;
  transition:linear all 0.40s;
  margin-left:10px;
 }
 div ul li a:hover{
  color:black;
 }



 #navBar {
   float: left;
 }
<!DOCTYPE HTML>
<html lang = "en">
  <head>
  <title>Videos</title>
      <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="videos.css">
  </head>


<body>
   <header>
  <nav>
  <h1> Main logo </h1>
   <ul id="nav"> 
    <li><a class="homeblack" href="index.html">Home</a></li>
    <li><a class="homered" href="videos.html">Videos</a></li>

 </ul>
 </nav>
 </header>
 <div class="divider"></div>
 <div id="navBar">

<ul>
 <li><a href="#">education videos </a></li>
 <li><a href="#">film videos</a></li>
 <li><a href="#">other videos </a></li>
</ul>

 </div>
<video title="abcd" controls width="250" height="160"> 
<source  src="videoplayback.mp4" />
</video> 
<video title="abcd" controls width="250" height="160"> 
<source src="videoplayback (1).mp4" />
</video> 

<video title="abcd" controls width="250" height="160"> 
<source src="1 Ton.mp4" /> 
</video> 

    <!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


  </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Woocommerce - [product_categories] shortcode - Can I exclude specific categories from being displayed in the list?

Is there a way to hide specific categories (only two in this case) that are displayed using the [product_categories] shortcode? I have attempted to place these categories at the bottom of the list and used CSS to target them: .home .woocommerce ul.product ...

Guide on how to prevent Paste (Ctrl+V) and Copy (Ctrl+C) functionalities in a TextField within MaterialUI

Is there a way to restrict the Paste (Ctrl+V) and Copy (Ctrl+C) functions in a specific TextField within MaterialUI? I am currently working with ReactJs ...

By utilizing the <tr> element, one can enhance the border thickness by 1 pixel

Is it possible to ensure that row selection in a table is consistent in terms of border alignment on both sides? https://i.stack.imgur.com/qmZiQ.png If you have any suggestions or solutions, please share them. Thank you! table { margin: 10px; bord ...

Next.js version 13 is causing the page to refresh each time the router is pushed

I am currently developing a search application using NextJs 13, and I have encountered an issue where the page refreshes every time I click the search button. Strangely, this only happens when the application is deployed on Vercel. When running the app l ...

What could be causing the flex item to be wider than its container?

One issue that can arise when utilizing flexbox is the scenario where, as demonstrated below, the content exceeds the width of the viewport causing the flexbox items to become wider than their container. What are the factors contributing to this situat ...

What is the standard error function used for jQuery promises?

Is there a way to establish a default error handling function for a jQuery promise? I am running a series of functions asynchronously, and if any of them encounter an error, I want the error to be reported. Currently, this is how I have to handle it: fun ...

Performing String formatting in JavaScript using an array

I've been utilizing the stringformat library to format strings in my node.js applications. var stringFormat = require('stringformat'); stringFormat.extendString(); In my current project, I'm attempting to pass an array of parameters a ...

Adding a characteristic to every item in an array of objects

Currently, I am utilizing Node.js along with Mongoose to interact with a MongoDB database and retrieve an array of objects from a specific collection. However, my aim is to add an additional property to each of these retrieved objects. Below, you can see t ...

Can you provide instructions on utilizing the async .update() function for DataTables within MDBootstrap?

I am just starting to explore MDBootstrap and I'm currently focused on grasping how to utilize the DataTables. While I've browsed through the examples on their website regarding Async table updates, I've found it a bit challenging to adapt i ...

Utilizing JSON API filtering within a Next.js application

Recently delving into the world of coding, I've embarked on a personal project that has presented me with a bit of a challenge regarding API filtering. My goal is to render data only if it contains a specific word, like "known_for_department==Directin ...

Challenges regarding placement and z-index are causing issues

Currently, I am attempting to make the menu overlap the content on my webpage. However, I am facing an issue where it moves the content box instead of overlapping it. I have already experimented with the position: relative concept, but unfortunately, the ...

What is the reason for triggering a rerender when there is a modification to a map() element using document.querySelector() in JS/next.js?

const slides = [ [string1, string2, stringi], [string1, string2, stringi], [string1, string2, stringi], [string1, string2, stringi], ]; const changeSlide = (num) => { const discipline = document.querySelector("#changeSlide-&quo ...

Is it feasible to run a function immediately following a reload?

Recently, I came across a code snippet that intrigued me: setTimeout(function() { window.location.reload(true); if ($scope.attribute.parentAttribute.id) { angular.element(document.getElementById($scope.attribute.parentAttribute.id)).click(); ...

Is it possible for search engines to crawl and index specific pages within a web application that is powered by

I have created a web application with a dynamic JavaScript interface that communicates with the server through AJAX requests. The content of the page is determined by the data after the hashtag in the URL, which is fetched from the server and displayed acc ...

Design a custom Bootstrap dropdown using an input[type="text"] element

After exploring the Bootstrap dropdown example here, I realized that for my particular scenario, it would be more beneficial to have an input field (type="text") instead of a button. This way, I can display the selected option from the dropdown. Is there ...

Create a dynamic form using JSON data and the Material UI library

Looking for assistance in creating a dynamic form on Next.js by parsing JSON files and generating the required components from the JSON data. Additionally, seeking guidance on utilizing the Material UI library for styling. Any examples or help would be g ...

What could be causing my video not to display a thumbnail on mobile devices until it is played?

When I view my website on mobile devices, there is no background image displayed before playing a video. Below is the HTML code I am using: <div class="row " > <div class="col-12 text-center"> <video control ...

Leverage React.js by incorporating a Header-Imported JavaScript Library

I recently developed a React.js web application and am exploring the use of Amplitude Analytics, which provides a Javascript SDK found here. According to the guidelines, I need to include a <script></script> within the <head></head> ...

Merge two distinct arrays of objects based on a shared field

I have two arrays of objects that I need to merge, with the expected output as: [ { "scenario": [ { "errorname": "Error 01", "status": 5, "desc_1" : "test", "desc_2" : "testing" }, ...

Adjust the width of a div within a nested iframe using jQuery without the need for an identifying ID on the iframe itself

Below is the code sample I'm working with: <iframe> <div id="inner_div"> Here's some text! </div> </iframe> I am aware that by adding an id to the iframe, I could follow the instructions provided here to access t ...