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

后端 未结 5 1492
佛祖请我去吃肉
佛祖请我去吃肉 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:40

    if you mean just the html then you can use jQuery to do get the number of characters (bytes in most cases, depending on the encoding)

    $(document).ready(
        function()
        {
            var pagebytes = $('html').html().length;
            var kbytes = pagebytes / 1024;
        }
    );
    

    this basically counts number of characters contained within (including) tag. that will exclude any Doctype specified, but since that would be static always you can add length of doctype to pagebytes.

    //Edit

    looks like doctype in the end may not be static. but without it, it still should be accurate enough.

提交回复
热议问题