View Full Version : Retrieve time used to load page

05-30-2007, 07:24 AM
Hi everyone! is there a way you can retrieve the time spent for a page (either HTML, ASPX or PHP) in seconds

<script language="Javascript">

var PageLoadTime;

PageLoadTime = window.loadingtime;

document.write("This page took" + PageLoadTime + "sec, to load")


05-30-2007, 08:12 AM
<?php $t1 = time().'.'.microtime(); ?>
<?php echo (time().'.'.microtime()) - $t1; ?>

That would give you processing time for the page, for PHP, but not loading time for the user.

For that, you could echo time() at the start of the script (once the page is requested), then use javascript onLoad in the body tag to check how long it has been.

there might be a better method.

It's especially hard, though, to tell from when you actually click a link, unless that link sends the time with it, which could be possible using javaascript, perhaps.

05-30-2007, 09:00 AM
thanks for help, but I would be more happy if you could help me using plain Javascript.

05-30-2007, 09:34 AM
That's not going to do anything for you, really.

1. User requests a page.
2. Server processes request.
3. Server prepares page (this is when PHP runs).
4. Server sends page to user.
5. User's computer displays page and runs scripts on page (javascript now).

Javascript would then just tell you how long it took... since it was done.

05-30-2007, 09:44 AM
ooh thanks man!!!

05-30-2007, 10:31 AM
Of course the one exception there is that something on the page might load, so you COULD use javascript to check how long it took for a particular image to load or to check how long it took for a scrolling area on the page to scroll, etc., but all of this is after the page has loaded.

05-30-2007, 03:40 PM
The time required by the browser to load the page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var begin=new Date().valueOf();
var end=new Date().valueOf();
alert('This page took '+[end-begin]+' milliseconds to load');


Notes: Must be the first thing in the head as soon as scripts are allowed there, as shown. Time will only be approximate as it doesn't include loading of meta data or title, etc. But, these usually take very little time.

Also, this is just the concept. Any particular implementation could, and probably should be a bit different.