PDA

View Full Version : content load timer


megahertzman
08-09-2002, 06:50 AM
does n e 1 know where i can find a javascript which will tell me on an HTML page how long exactly it took for the page to load.
thanx
email hoth83@hotmail.com

beetle
08-09-2002, 08:26 AM
Maybe like this<html>
<head>
<script>
// This first part should be at top of page, before much other HTML is loaded
var d = new Date()
var time = d.getTime();

// function can go anywhere
function getPageLoad()
{
var d2 = new Date()
var time2= d2.getTime();
var lTime = time2-time;
alert("Page loaded in "+lTime+" milliseconds");
}

</script>
</head>
<body onLoad="getPageLoad()">

megahertzman
08-09-2002, 07:46 PM
Thanks, Beetle, but I want it to be so that I could write it out to the page and not be an alert box. I want to right it out like onto a little corner on my page.

Thanks, though. If you could find what I'm looking for, please reply.

Haider

(I tried playing with it to get what I want but no success.)





:p

beetle
08-09-2002, 07:52 PM
Well, it doesn't have to be alerted. You've got the time in the variable lTime so just use it with document.write() in any place you'd like. Just declare lTime from outside the function before setting, so it will be global to the page.

megahertzman
08-09-2002, 08:13 PM
How do I do that?

Is it:

<script="JavaScript">

document.write(lTime);

</script>

?

beetle
08-09-2002, 08:17 PM
Exactly!

Just remember to declare the variable BEFORE the function that is becomes global to the page....

megahertzman
08-09-2002, 08:21 PM
I did! I did! But it still won't print to screen!

This is how it looks (and btw, I guessed!):

In the <HEAD> and </HEAD> tags:

<script>
var d = new Date()
var time = d.getTime();
var lTime;

function getPageLoad()
{
var d2 = new Date()
var time2= d2.getTime();
var lTime = time2-time;
alert("Page loaded in "+lTime+" milliseconds");
}

</script>

On the page:

<script="JavaScript">
document.write(lTime);
</script>

What seems to be the problem?

beetle
08-09-2002, 08:27 PM
Uh, try declaring lTime as an integer from the start

var lTime = 0;

EDIT: You don't need to re-declare the variable on this line: var lTime = time2-time;

megahertzman
08-09-2002, 08:32 PM
Nope, still don't work! I tried setting the initial value of the variable to 0.

Where ever I put this into my HTML:
<script="JavaScript">
document.write(lTime);
</script>

I get this printed onto the screen instead:
document.write(lTime);

Yes, I removed the var in front of lTime, still no success.

Hmmmmm.....

beetle
08-09-2002, 08:38 PM
Ok, I see the prob. You script line is wrong...

<script="JavaScript">
-- Should Be --
<script language="JavaScript">

Remember, programming is 90% spelling and grammar, 10% logic :D

megahertzman
08-09-2002, 08:42 PM
90% grammer, wow!

I plugged in the 'language', but know instead it ALWAYS outputs to the screen '0'.

It's probably because I initialized it to the value 0. But still it should subtract and do the calculations. Hmmm.

I tried removing the initial value and then it outputs to the screen:
'undefined'.

:confused:

Oh, boy. And I plan to major in this!

megahertzman
08-09-2002, 08:44 PM
Oh yeah, and can I remove this line from the function:

alert("Page loaded in "+lTime+" milliseconds");

It's no use cuz I don't want to alert. Right?

beetle
08-09-2002, 09:13 PM
Ok, I've figured out what was inheritly wrong with writing the item to the page. The function getPageLoad() doesn't run until the page is fully loaded, however, we were trying to access the variable it sets WITHIN the body of the page, or, before the body was completely loaded. So, here's a solution

http://www.lanwizards.com/test.htm

View the source on that page....

megahertzman
08-09-2002, 10:42 PM
Thanks, I got it, although I'm still trying to figure out the reason why!
:thumbsup: