Read the size of the current document from Javascript

I am starting to play with Boomerang to measure performance. This is very promising. Although this allows me to measure the latency, bandwidth, and page load time, I'm also interested in trying to get the time spent rendering the original HTML page on the server side. We will see that just checking the time when the browser started parsing javascript (which is close to when it started arriving) in order to get an estimate of the server time, I need to find out how much network time to subtract. To do this, I need to know how big the html document is.

How can I say this from Javascript?

The document object does not have an innerHtml property

I tried

totsize=document.HEAD.innerHTML.length + document.BODY.innerHTML.length;

HEAD and BODY objects are displayed in the DOM browser in Firefox, but when I try to execute the code above, I get an undefined error - also tried with a "head" - to no avail.

Any ideas?

(note that javascript is in a separate file and will be cached in most cases - so this is not a big problem).

I tried google - but I just get a lot of pages describing the html screen size on the screen and window sizes :(

TIA

+3
source share
3 answers

How about document.documentElement.innerHTML.length?

+4
source

document.documentElement.innerHTML.lengthaccording to @meder is probably good enough. This will be inaccurate because it returns the serialization of the current DOM page, not the original HTML source.

, , , , ... , script, innerHTML, , , .

, , , XMLHttpRequest URL-:

var xhr= new XMLHttpRequest();
xhr.onreadystatechange= function() {
    if (this.readyState!==4) return;
    alert(this.responseText.length);
};
xhr.open('GET', location.href, true);
xhr.send();

( , POST.)

. , ​​ ISO-8859-1, . UTF-8, , UTF-8- , :

var bytes= unescape(encodeURIComponent(this.responseText));
alert(bytes.length);

, , UTF-8, ; , .

+2

?:

/*
* This script determines the size of the current page in bytes.
* But why does the 2nd length differ from the first?
*/
var len=document.getElementsByTagName("html")[0].innerHTML.length;
var len2=document.getElementsByTagName("head")[0].innerHTML.length + document.getElementsByTagName("body")[0].innerHTML.length;
//document.body.innerHTML.length
alert('Page Size: '+len +' bytes');
alert('Page Size2: '+len2+' bytes');

, , . . ? ! .

EDIT: Can anyone fix this formatting? My browser seems to be malfunctioning. Firefox 15.x

EDIT: fixed formatting

0
source

Source: https://habr.com/ru/post/1779449/


All Articles