HTML Progress Bar Using JavaScript Source Code

In this tutorial we will create a HTML Progress Bar using JavaScript. This code will dynamically display an animated progress bar when the user click the button. The code use onclick() function to call a specific method that will animate the progress bar by incrementing the width properties of an element and will stop if it reach the maximum width with setInterval(). This is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as shown below. index.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.                 <link rel="stylesheet" type="text/css" href="css/style.css"/>
  7.         </head>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">HTML Progress Bar Using JavaScript Source Code</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-6">
  18.                         <button class="btn btn-primary" onclick="startButton(this);"> PRESS ME!</button>
  19.                         <br /><br />
  20.                         <div id="result" style="display:none;">
  21.                                 <div id="progressholder">
  22.                                         <div id="progress"></div>
  23.                                 </div>
  24.                                 <center><h4>Please Wait.....</h4></center>
  25.                         </div>
  26.                 </div>
  27.                
  28.        
  29.                
  30.         </div>
  31. </body>
  32. <script src="js/script.js"></script>
  33. </html>
home.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">HTML Progress Bar Using JavaScript Source Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                
  18.                 <div class="col-md-8">
  19.                         <center><h2>Please visit here <a href="https://sourcecodester.com">link</h2></center>
  20.                 </div>
  21.                
  22.        
  23.                
  24.         </div>
  25. </body>
  26. </html>

Creating the Script

This code contains the script of the application. This code will start an animated progress bar when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function startButton(btn){
  2.         document.getElementById('result').style.display="inline";
  3.         aniProgress();
  4.         btn.style.display="none";
  5. }
  6.  
  7. function aniProgress(){
  8.         var progress = document.getElementById('progress');
  9.         var width = 0;
  10.         var interval = setInterval(duration, 60);
  11.        
  12.        
  13.         function duration(){
  14.                 if(width >= 100){
  15.                         clearInterval(interval);
  16.                         window.location = 'home.html';
  17.                 }else{
  18.                         width++;
  19.                         progress.style.width = width + '%';
  20.                 }
  21.         }
  22.        
  23. }
There you have it we successfully created a HTML Progress Bar using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment