Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a running progress bar while page is loading

I want to show a running progress bar while my page is loading like here, in my page. I used a simple loading image in my example, but I want to convert it in a running progress bar. Here is my code:

$(window).load(function() {      alert("hi");      $('#loading').hide();  });
#loading {      width: 100%;      height: 100%;      top: 0px;      left: 0px;      position: fixed;      display: block;      opacity: 0.7;      background-color: #fff;      z-index: 99;      text-align: center;  }    #loading-image {      position: absolute;      top: 100px;      left: 240px;      z-index: 100;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>  <div id="loading">      <img id="loading-image" src="http://cdn.nirmaltv.com/images/generatorphp-thumb.gif" alt="Loading..." />  </div>  <div id="txt">      <h2>Let AJAX change this text</h2>  </div>  <button>Change Content</button>

Here is a JSFiddle

like image 309
amit gupta Avatar asked Sep 24 '13 12:09

amit gupta


People also ask

How do I display the progress bar in HTML?

Use the <progress> tag to create a progress bar in HTML. The HTML <progress> tag specifies a completion progress of a task. It is displayed as a progress bar. The value of progress bar can be manipulated by JavaScript.

How do I put the progress bar on top?

To get started, add the following markup to the index. html file right after the opening <body> tag. fixed-top is a Bootstrap utility class that adds the following CSS styling to an element. With that, the Progress Indicator will stay on top of the web page, even when the user scrolls.

Which bar displays the progress bar?

In this example, modeless progress is shown in the address bar. Otherwise, if the window has a status bar, display the modeless progress in the status bar. Put any corresponding text to its left in the status bar. In this example, modeless progress is shown in the status bar.


2 Answers

I have copied the relevant code below from This page. Hope this might help you.

$.ajax({   xhr: function() {     var xhr = new window.XMLHttpRequest();     //Upload progress     xhr.upload.addEventListener("progress", function(evt) {       if (evt.lengthComputable) {         var percentComplete = evt.loaded / evt.total;         //Do something with upload progress         console.log(percentComplete);       }     }, false);     //Download progress     xhr.addEventListener("progress", function(evt) {       if (evt.lengthComputable) {         var percentComplete = evt.loaded / evt.total;         //Do something with download progress         console.log(percentComplete);       }     }, false);     return xhr;   },   type: 'POST',   url: "/",   data: {},   success: function(data) {     //Do something success-ish   } }); 
like image 108
Tapas Pal Avatar answered Sep 19 '22 23:09

Tapas Pal


It’s a chicken-and-egg problem. You won’t be able to do it because you need to load the assets to display the progress bar widget, by which time your page will be either fully or partially downloaded. Also, you need to know the total size of the page prior to the user requesting in order to calculate a percentage.

It’s more hassle than it’s worth.

like image 35
Martin Bean Avatar answered Sep 22 '22 23:09

Martin Bean