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.
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).
-
1
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.
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.
-
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 May 20 '09 at 21:02
-
1I've done exactly what you're trying to do and you HAVE to use a file of a known size - typically an image. – annakata May 20 '09 at 21:11
-
ah, ok, so the widget will contain for example a fixed size image and will calculate comparing to it. – Amr Elgarhy May 20 '09 at 21:14
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";
To get the file size of pages on the web I built a javascript bookmarklet to do the trick. It alerts the size of the page you're on in kb's. Not sure if it can help with connection speed though.
Change the alert to a prompt if you want to copy the filesize.
Here's the bookmarklet code for the alert.
<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);alert(c+' kb');">Doc Size</a>
Here's the bookmarklet code for the prompt.
<a href="javascript:a=document.getElementsByTagName('HTML')[0].outerHTML;b=a.length/1024;c=Math.round(b);prompt('Page Size',c+' kb');">Doc Size</a>
See it in action at http://bookmarklets.to.g0.to/filesize.php
-
thanks. This helped answer my question at stackoverflow.com/questions/33351977/… – anjanb Oct 27 '15 at 6:13
-
1
'file.dat?nc='+Math.random()
– Christian May 4 '12 at 14:10