How to get current page size in KB using just javascript?

后端 未结 5 1490
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 02:48

Referring to this question , how can i get the current page size in kb, this size i will use to know the connection speed for this page.

5条回答
  •  无人及你
    2020-12-06 03:21

    There is a non-standard property available only for IE - document.fileSize MDN MSDN. It returns the size of the html page in bytes.

    Or you can get the content-length header

    var request = new XMLHttpRequest();
    request.open('GET', document.location, false);
    request.send();
    var size = request.getAllResponseHeaders().toLowerCase().match(/content-length: \d+/);
    document.getElementById("size").innerHTML = size + " bytes";
    
    1. Example with text only;
    2. Example geting the size of image;
    3. Example with content-length

提交回复
热议问题