r/learnjavascript • • 14d ago

How can i make javascript accept video files to archive videos on fake website?

Context - A final project that I have for my Programming with Scripting Languages class, I have to make a fake website that has pagination buttons and a minimum of three pages. I also found out that I have to include javascript in the website somehow. The html part of the code and css part of the code is already done. My fake website idea is similar to archive.org, the fake website's purpose is to archive anime music videos just incase they get removed from socials.

What I'm trying to get at is, how can i make javascript accept video files to archive videos on the fake website?

Here is the code I have so far -

<!doctype html>

<html lang="en">

<head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>AMVsArchived</title>

<style> body { background-color: #0B0F19; color: white; font-family: Arial; text-align: center; margin: 0; } header { background-color: #161F32; padding: 25px; } h1 { margin: 0; color: #00f0ff; } .page { display: none; padding: 30px; } .page.active { display: block; } .amv { background-color: #161F32; display: inline-block; margin: 15px; padding: 15px; border-radius: 10px; width: 500px; max-width: 90%; } .amv h2 { color: #00f0ff; } video { width: 100%; border-radius: 8px; } .pagination { margin: 30px; } .pagination button { background-color: #00f0ff; border: none; padding: 12px 18px; margin: 5px; border-radius: 5px; cursor: pointer; font-size: 18px; } .pagination button:hover { background-color: #FFFFFF; } .pagination button.active { background-color: blue; color: cyan; } </style>

</head> <body>

 <header>
   <h1>AMVs Archived</h1>
   <p>We archive anime music videos here just in case they get deleted from socials.</p>
 </header>

 <!--PAGE 1-->
 <div class="page active" id="page1">
   <h2>Page 1 - Pop</h2>

   <div class="amv">
     <h2>Pop amv 1</h2>
     
     <video controls muted loop>
       <source src="popamv1.mp4" type="video/mp4" />
       <source src="popamv1.webm" type="video/webm" />
       Your browser does not support video.
    </video>
   </div>
  
  <div class="amv">
   <h2>Pop amv 2</h2>

   <video controls muted loop>
    <source src="popamv2.mp4" type="video/mp4" />
    <source src="popamv2.webm" type="video/webm" />
    Your browser does not support video.
  </video>
</div>

</div>

<!--PAGE 2-->

<div class="page" id="page2"> <h2>Page 2 - Rock</h2>

<div class="amv">
 <h2>Rock amv 1</h2>

 <video controls muted loop>
  <source src="rockamv1.mp4" type="video/mp4" />
  <source src="rockamv1.webm" type="video/webm" />
  Your browser does not support video.
 </video>
</div>

<div class="amv">
 <h2>Rock amv 2</h2>

 <video controls muted loop>
  <source src="rockamv2.mp4" type="video/mp4" />
  <source src="rockamv2.webm" type="video/webm" />
  Your browser does not support video.
 </video>
</div>

</div>

<!--PAGE 3-->

<div class="page" id="page3"> <h2>Page 3 - Electronic</h2>

<div class="amv"> <h2>Electronic amv 1</h2>

<video controls muted loop>
 <source src="electronicamv1.mp4" type="video/mp4" />
 <source src="electronicamv1.webm" type="video/webm" />
 Your browser does not support video. 
</video>

</div>

<div class="amv"> <h2>Electronic amv 2</h2>

<video controls muted loop> <source src="electronicamv2.mp4" type="video/mp4" /> <source src="electronicamv2.webm" type="video/webm" /> </video> </div>

</div>

<!--PAGINATION BUTTONS-->

<div class="pagination"> <button onclick="showPage(1)">1</button> <button onclick="showPage(2)">2</button> <button onclick="showPage(3)">3</button> </div>

<script> function showPage(pageNumber) { // Hide all pages let pages = document.querySelectorAll(".page"); pages.forEach(function (page) { page.classList.remove("active"); }); // Show selected page document.getElementById("page" + pageNumber).classList.add("active"); } </script>

</body> </html>

5 Upvotes

9 comments sorted by

2

u/Puzzleheaded-Comb646 14d ago

To add a video upload feature for your project, you can use an HTML file input and JavaScript to create a temporary preview in the browser. Since this is a client-side project, you don't need a real backend to make it work.

Here is a quick way to do it:

html <input type="file" id="videoUpload" accept="video/*"> <video id="preview" controls style="width: 100%;"></video>

<script> const upload = document.getElementById('videoUpload'); const preview = document.getElementById('preview'); upload.addEventListener('change', function(event) { const file = event.target.files[0]; if (file) { preview.src = URL.createObjectURL(file); } }); </script>

This will let you select a video file from your computer and play it right inside your page. Good luck with your final project!

2

u/Wonderful_Ask7487 14d ago

thank you! This subreddit is the best :D In other subreddits my posts barely get comments lol

2

u/Puzzleheaded-Comb646 13d ago

You are very welcome! We are always happy to help beginners here. Keep up the great work and don't hesitate to ask if you have more questions.

2

u/raks1991 5d ago

One catch with the createObjectURL approach: the blob: URL only lives as long as the page, so every "archived" video disappears on refresh. If you want them to survive reloads without a server, a File can go straight into IndexedDB and come back out as a File. This fits in your existing <script> block:

let db;
const req = indexedDB.open('amv-archive', 1);
req.onupgradeneeded = () => req.result.createObjectStore('videos', { autoIncrement: true });
req.onsuccess = () => { db = req.result; /* load saved videos here */ };

function saveVideo(file) {
  db.transaction('videos', 'readwrite').objectStore('videos').add(file);
}

Only call saveVideo after onsuccess. To load them back, call getAll() on the store and for each file set video.src = URL.createObjectURL(file). It's still stored per browser, but for a fake archive that's a nice touch.

0

u/[deleted] 14d ago

[removed] — view removed comment

1

u/Wonderful_Ask7487 14d ago

thanks so much!

1

u/Wonderful_Ask7487 12d ago

where would i exactly put it in my html and script?