Javascript - Simple Loading Bar

In this tutorial we will create a Simple Loading Bar using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that has been used for the layout https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display the form that has a loading bar. To create this just write these block of code inside the text editor and save this as 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.  
  7. #loadingholder{
  8. width: 100%;
  9. background-color:#ddd;
  10. }
  11.  
  12. #loading{
  13. width: 0%;
  14. height:30px;
  15. background-color:#337AB7;
  16. }
  17.  
  18. </style>
  19.  
  20. </head>
  21. <nav class="navbar navbar-default">
  22. <div class="container-fluid">
  23. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester.com</a>
  24. </div>
  25. </nav>
  26. <div class="col-md-3"></div>
  27. <div class="col-md-6 well">
  28. <h3 class="text-primary">Javascript - Simple Loading Bar</h3>
  29. <hr style="border-top:1px dotted #ccc;"/>
  30. <div class="col-md-2"></div>
  31.  
  32. <div class="col-md-8">
  33. <div id="loadingholder">
  34. <div id="loading"></div>
  35. </div>
  36. <center><h4>Now Loading...</h4></center>
  37. </div>
  38.  
  39.  
  40.  
  41. </div>
  42. </body>
  43. <script src="js/script.js"></script>
  44. <script type="text/javascript">
  45. Progress();
  46. </html>
Next, we will create the home page after the load is finished Create another text file then write these block of codes inside the newly created text file then called it 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.com</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascript - Simple Loading Bar</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>Welcome</h2></center>
  20. <center><h2>To</h2></center>
  21. <center><h2>Sourcecodester</h2></center>
  22. </div>
  23.  
  24.  
  25.  
  26. </div>
  27. </body>
  28. </html>

Creating the Script

This code contains the script of the application. To do this just create a new directory called js then copy and write these block of codes as shown below inside the text editor and save it as script.js in the newly created directory.
  1. function Progress(){
  2. var loading = document.getElementById('loading');
  3. var width = 0;
  4. var interval = setInterval(Duration, 60);
  5.  
  6.  
  7. function Duration(){
  8. if(width >= 100){
  9. clearInterval(interval);
  10. window.location = 'home.html';
  11. }else{
  12. width++;
  13. loading.style.width = width + '%';
  14. }
  15. }
  16. }
There you have it we successfully created a Simple Loading 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