how to get length/size of a webpage using javascript

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 13:48:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!