Call (877) SITE-OPT (748-3678)

Code: Monitor Page Rendering Time

Chapter 8 - Ajax Optimization

You can use JavaScript to see how long it takes a page to load. For example, at the top of an HTML document, start a script timer:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>And they're off...</title>
<script type="text/javascript">
    var gPageStartTime = (new Date( )).getTime( );

Then bind a script to stop the timer upon full-page load to calculate how long it took:

window.onload = function ( )
    var pageEndTime = (new Date( )).getTime( );
    var pageLoadTime = (pageEndTime - gPageStartTime)/1000;
    alert("Page Load Time: " + pageLoadTime);

Of course, instead of alerting the page load time as we did here, we could transmit it to our server with an Ajax request:

sendAjaxRequest("", "time="+pageLoadTime);
Copyright © 2002-2018 Website Optimization, LLC. All Rights Reserved - Free website speed test - Privacy Policy
Last modified: February 19, 2009.

Follow us on: Twitter, Google+, Facebook, Linked In