views:

574

answers:

3

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.

+4  A: 

I think grabbing a known data file via Ajax would be a better solution than measuring the current page. It's easier to time the start and completion of an Ajax call.

Diodeus
I am going to make this as a widget where other users can put on their website, and want to calculate speed their browsers not on the server
Amr ElGarhy
I've done exactly what you're trying to do and you HAVE to use a file of a known size - typically an image.
annakata
ah, ok, so the widget will contain for example a fixed size image and will calculate comparing to it.
Amr ElGarhy
+2  A: 

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.

Michal M
+3  A: 

Unless your page has size in megabytes, the result will be meaningless.

This is because time needed to connect, send request, and wait for server to send reply back is quite large compared to time required to download the page, and in addition to that TCP/IP has slow start.

You also have to take into account caches, proxies and number of parallel connections that browser will make (e.g. may prioritize download of scripts and styles, making page download time appear slow).

porneL
Combine this answer with Diodeus's answer and there you go!
Allen