问题
We get the webpage timing info using w3c navigation API.
we get the resource information using the resource timing API.
Now, how to get the size of the webpage. It appears that if I know when the page loaded, etc, I should know when the last byte was downloaded -- this should be enough to give me the length/size of the page.
How do I get this length/size ?
回答1:
Found the answer at : How to get current page size in KB using just javascript?. User @Stev has answered it
document.getElementsByTagName('HTML')[0].outerHTML.length;
回答2:
Are you looking for the height of the browser window?
window.innerHeight;
window.innerWidth;
回答3:
Here you go:
Math.ceil($('html').html().length / 1024) + "KB";
回答4:
Since HTML files are pretty much just text files, you could count how many characters you have and that would be your web page size in bytes, assuming you have 1 character = 1 byte. Then, divide by a multiple of 1024 to get KB, MB, GB etc.
$("html").html().length / (n * 1024)
Edit with javascript only :
Shortcut to retrieve the root element of the document source
document.documentElement.outerHTML.length
or
Retrieve the array of elements who's tag name is "html", assuming the first one is your root element (true for all valid HTML files), and count length :
document.getElementsByTagName("html")[0].outerHTML.length
来源:https://stackoverflow.com/questions/33351977/how-to-get-length-size-of-a-webpage-using-javascript