Omar Abid Omar Abid - 6 months ago 24
jQuery Question

page load time with Jquery

I want to calculate the page load time; This means from second 0 (a little jquery snippet was loaded) to second x, when the whole page is loaded.

i wonder if any one had an experience with it, also ideas how to implement it correctly will be apperciated.

please i don't need an extension, i already have firebug, i need a js solution

thanks :)

Answer

As others have mentioned, this is not going to be terribly accurate. But this should work reasonably.

In your <head>, i.e., as early as possible:

<script>
   var startTime = (new Date()).getTime();
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
   $(window).load(function () {
       var endTime = (new Date()).getTime();
       var millisecondsLoading = endTime - startTime;
       // Put millisecondsLoading in a hidden form field
       // or Ajax it back to the server or whatever.
   });
</script>

The key is this behavior, from the jQuery docs:

When bound to the window element, the event fires when the user agent finishes loading all content within a document, including window, frames, objects and images.

Comments