...

View Full Version : window.onload vrs <body onload="">



Chris_S
03-16-2009, 11:09 AM
Hi,

I have a div element that contains some text. I need to know the height of this text before the page is displayed and I'm trying to find the quickest method possible.

1. Is body onload faster then using window.onload?
2. Do these events get executed before any of the page is displayed?
3. Can you use body onload in a external javascript file instead of in the <body> tag?

The reason I'm wondering is I don't want the elements to 'flicker' on the page after I set style properties using javascript. The page should display properly immediately without any changes.

Thanks

Philip M
03-16-2009, 12:00 PM
1. Is body onload faster then using window.onload?
2. Do these events get executed before any of the page is displayed?
3. Can you use body onload in a external javascript file instead of in the <body> tag?



1) I would have thought there was no difference.
2) No. onload means execute after loading has completed.
3) No. body onload can only be used in the <body> tag. You can use window.onload anywhere but of course multiple onload statements will fail.


See: - http://www.javascriptkit.com/javatutors/multiplejava.shtml


You can get the <div> height as follows, but of course the <div> must exist when the script runs:-

<script type="text/javascript">
function getMyDivHeight() {
var boxHeight = document.getElementById("mydiv").clientHeight;
alert(boxHeight);
}
</script>

“What is a cynic? A man who knows the price of everything and the value of nothing.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

Chris_S
03-16-2009, 12:29 PM
Yea I didn't think there was any difference, was just making sure.

This is the way I have my page setup so far. Is this a good way or is there a better/faster solution?

1. The body is hidden in the external css
2. window.onload
3. get height of div using document.getElementById('txt').offsetHeight
4. once height is found I unhide the body.

I see you used clientHeight...gotta look into that property.


I dunno I would rather get the size of the div before it gets loaded. hmm is this even possible?

Philip M
03-16-2009, 01:03 PM
I see you used clientHeight...gotta look into that property.

For difference between offsetHeight and clientHeight see:-
http://msdn.microsoft.com/en-us/library/ms533563(VS.85).aspx


I dunno I would rather get the size of the div before it gets loaded. hmm is this even possible?


It is as possible as to make yourself rise up from the ground by pulling on your hair.

freedom_razor
03-16-2009, 06:17 PM
You can script ONLOAD for IMG and SCRIPT tags, so for the fastest possible way to get that DIV size, you could include a SCRIPT or IMG [1x1px I suppose] just after the DIV, and use ONLOAD on any of those tags to check DIV size. That way you can get the size right after DIV loads and don't have to wait for the rest of the document.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum