Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the html document size with js?

How can you get the actual file size(not in pixels,in bytes) of the document a js script is running on? The solution should work in all major browsers.

like image 556
nope Avatar asked Nov 28 '25 21:11

nope


2 Answers

//

function hrefSize(){
    try{
        var O= new XMLHttpRequest;
        O.open("HEAD", document.URL, false);
        O.send(null);
        if(O.status== 200){
            return O.getResponseHeader('Content-Length');
        }
        else return 0;
    }
    catch(er){
        return 0;
    }
}
alert(hrefSize()+' bytes');
like image 148
kennebec Avatar answered Dec 01 '25 09:12

kennebec


You could propably refetch the page via ajax and get the length of it as a string, but this will be suboptimal because it will trigger another HTTP request and have to wait for it.

like image 30
thejh Avatar answered Dec 01 '25 09:12

thejh