简体   繁体   English

如何使用javascript获取网页的长度/大小

[英]how to get length/size of a webpage using javascript

We get the webpage timing info using w3c navigation API. 我们使用w3c导航API获取网页计时信息。
we get the resource information using the resource timing API. 我们使用资源计时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 ? 我如何获得这个长度/尺寸?

Found the answer at : How to get current page size in KB using just javascript? 找到答案: 如何使用javascript以KB为单位获取当前页面大小? . User @Stev has answered it 用户@Stev已经回答了它

document.getElementsByTagName('HTML')[0].outerHTML.length;

Are you looking for the height of the browser window? 您在寻找浏览器窗口的高度吗?

window.innerHeight;
window.innerWidth;

干得好:

Math.ceil($('html').html().length / 1024) + "KB";

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. 由于HTML文件几乎只是文本文件,因此您可以计算您拥有的字符数,这将是您的网页大小(以字节为单位),假设您有1个字符= 1个字节。 Then, divide by a multiple of 1024 to get KB, MB, GB etc. 然后,除以1024的倍数,得到KB,MB,GB等。

$("html").html().length / (n * 1024)

Edit with javascript only : 仅使用javascript进行修改

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 : 检索标记名称为“html”的元素数组,假设第一个元素是您的根元素(对于所有有效的HTML文件都是true),并且计数长度:

document.getElementsByTagName("html")[0].outerHTML.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取网页资源的大小,例如javascript,css,图片等 - How to get the Size of a webpage resource like javascript, css, image, etc 如何在不使用本机长度方法的情况下在 javascript 中获取字符串的长度 - How to get length of string in javascript without using native length method 如何使用javascript从网页获取JSON对象数据 - How to get JSON object data from a webpage using javascript 如何使用php和javascript获取网页中显示图像的路径 - how to get the path of a displayed image in a webpage using php and javascript 如何使用Javascript从网页获取图像数据 - How to get hold of image data from webpage using Javascript 如何使用WebBrowser在以JavaScript编写的网页中获取超链接 - How to get the hyperlink in a webpage written in JavaScript using a WebBrowser 如何使用javascript或jquery获取网页的屏幕截图? - How to get screen shot of webpage using javascript or jquery? 如何使用JavaScript在网页上获取IOS配音的选定元素? - How to get the selected element of the IOS voiceover on a webpage using JavaScript? 使用javascript通过字符长度增加字体大小 - Increase font size by character length using javascript 如何使用javascript以字节为单位获取图像大小 - How to get image size in bytes using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM