JavaScript - Simple HTML Progress Bar

In this tutorial we will create a Simple HTML Progress Bar using JavaScript. This code will automatically display an animated progress bar when the user click the launch button. The code use onclick() to launch the animated progress bar by incrementing the width properties of an element again and again until it reach the maximum width by using 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. #progressholder{
  7. width: 100%;
  8. background-color:#ddd;
  9. }
  10.  
  11. #progress{
  12. width: 0%;
  13. height:30px;
  14. background-color:green;
  15. }
  16.  
  17. </style>
  18.  
  19. </head>
  20. <nav class="navbar navbar-default">
  21. <div class="container-fluid">
  22. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  23. </div>
  24. </nav>
  25. <div class="col-md-3"></div>
  26. <div class="col-md-6 well">
  27. <h3 class="text-primary">JavaScript - Simple HTML Progress Bar</h3>
  28. <hr style="border-top:1px dotted #ccc;"/>
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6">
  31. <button class="btn btn-primary" onclick="launchProgress();"> Launch Progress Bar</button>
  32. <br /><br />
  33. <div id="result" style="display:none;">
  34. <div id="progressholder">
  35. <div id="progress"></div>
  36. </div>
  37. <center><h4>Going to Home Page</h4></center>
  38. </div>
  39. </div>
  40.  
  41.  
  42.  
  43. </div>
  44. </body>
  45. <script src="js/script.js"></script>
  46. </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. #progressholder{
  7. width: 100%;
  8. background-color:#ddd;
  9. }
  10.  
  11. #progress{
  12. width: 0%;
  13. height:30px;
  14. background-color:green;
  15. }
  16.  
  17. </style>
  18.  
  19. </head>
  20. <nav class="navbar navbar-default">
  21. <div class="container-fluid">
  22. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  23. </div>
  24. </nav>
  25. <div class="col-md-3"></div>
  26. <div class="col-md-6 well">
  27. <h3 class="text-primary">JavaScript - Simple HTML Progress Bar</h3>
  28. <hr style="border-top:1px dotted #ccc;"/>
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6">
  31. <button class="btn btn-primary" onclick="launchProgress();"> Launch Progress Bar</button>
  32. <br /><br />
  33. <div id="result" style="display:none;">
  34. <div id="progressholder">
  35. <div id="progress"></div>
  36. </div>
  37. <center><h4>Going to Home Page</h4></center>
  38. </div>
  39. </div>
  40.  
  41.  
  42.  
  43. </div>
  44. </body>
  45. <script src="js/script.js"></script>
  46. </html>

Creating the Script

This code contains the script of the application. This code will launch a 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 progressBar(){
  2. var progress = document.getElementById('progress');
  3. var width = 0;
  4. var interval = setInterval(progressDuration, 60);
  5.  
  6.  
  7. function progressDuration(){
  8. if(width >= 100){
  9. clearInterval(interval);
  10. window.location = 'home.html';
  11. }else{
  12. width++;
  13. progress.style.width = width + '%';
  14. }
  15. }
  16.  
  17. }
  18.  
  19. function launchProgress(){
  20. document.getElementById('result').style.display="inline";
  21. progressBar();
  22. }
There you have it we successfully created a Simple 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