When you load a page in browser, it shows the percentage loaded while loading the page. Same with when you do an AJAX request with XMLHttpRequest (XHR), it allows you to see the loading percentage.
I'm just curious how such feature is been achieved behind the scene. How does client know the data size it will receive beforehand? Personally, when I create my web server using Node.js, I didn't manually implement such capability like sending the client the percentage loaded on every interval of milliseconds, etc.
Just how does this all work?
enter image description here
-
i'm in no way an expert on this, but i think that the percentage loaded is just the percentage of the website that has been RENDERED. When the browser retrieves data from the server, it knows the exact size, and then just updates the remaining data after each element has been rendered. But i'm not sure.Caweren– Caweren2014年07月23日 06:14:55 +00:00Commented Jul 23, 2014 at 6:14
-
1Probably by reading Content-Length entity-header?Ram– Ram2014年07月23日 06:15:06 +00:00Commented Jul 23, 2014 at 6:15
1 Answer 1
When a browser requests a resource from a server, in most cases the server replies with the size of the requested resource, before the download begins. For Example:
HTTP/1.1 200 OK
Date: 2005年5月23日 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: 2003年1月08日 23:11:55 GMT
ETag: "3f80f-1b6-3e1cb03b"
Content-Type: text/html; charset=UTF-8
Content-Length: 131
Accept-Ranges: bytes
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
In the above server response, the length of the requested resource is given, so the client will know how many bytes to read. This allows the client to display a meaningful progress indicator.
1 Comment
Explore related questions
See similar questions with these tags.