/    Sign up×
Community /Pin to ProfileBookmark

cant detect the size of DIV

hello,

i wan to detect a div size, below is my code

<div id=”top” class=”top” ><img src=’http://domain.com/images/1.jpg’></div>
<div id=”botoom” class=”bottom” ><img src=’http://domain.com/images/2.jpg’></div>

<script type=”text/javascript”>
var divh = document.getElementById(‘top’).offsetHeight;
if (divh >=50)
{
alert(divh);
}
</script>

i dun know what happen, seems like i have to load twice then i get the allert.
if i clear my cache and load the this page. it wont alert me until i refresh again. seems like it cant get the size on the div in 1st time load.

Any idea?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@steveaJul 12.2007 — That's because the code is executing before the page has been rendered, try this:
<i>
</i>window.onload = function (){
var ele = document.getElementById('top');
if(ele.offsetHeight &gt;= 50)
alert(divh);
};
Copy linkTweet thisAlerts:
@kennyTEauthorJul 12.2007 — thanks for the quick reply.

Correct me if i'm wrong.

window onload have have to wait all thing to load 1st right?

if i have multiple picture and i want to alert in sequence. how?

something like, div 1 pic one already load, alert. then div 2 pic two already load, alert again? i want to do this because sometimes the user quit the page before the they load the second image.

How?
Copy linkTweet thisAlerts:
@kennyTEauthorJul 12.2007 — stevea,

I've try ur code, seems does't function.

<script type="text/javascript">

document.write("<div id='top' class='top' ><img src='http://DOMAIN.com/images/1.jpg'></div>");

window.onload = function (){

var ele = document.getElementById('top');

if(ele.offsetHeight >= 50)

alert(divh);

};

</script>

what happen?
Copy linkTweet thisAlerts:
@felgallJul 12.2007 — The onload has to be in the head of the page.

All document.write() statements have to be either in the body of the page or in functions called from the body.
Copy linkTweet thisAlerts:
@kennyTEauthorJul 12.2007 — i do know that The onload has to be in the head of the page.

below is my code.
===============================================


<div id='top' class='top' ><img src='http://domain.com/images/1.jpg'></div>

<div id='center' class='center' ><img src='http://domain.com/images/2.jpg'></div>

<div id='bottom' class='bottom' ><img src='http://domain.com/images/3.jpg'></div>

<script type="text/javascript">

window.onload = function(){

var ele = document.getElementById('top');

if(ele.offsetHeight > 25)

alert(ele.offsetHeight);

};

</script>
================================================



How can i alert when the 1st picture already output out to user?

as i know if there is a picture, it will assume the div is bigger than 25px.

i need to alert one by one, not wait till all the image been porpulate.
×

Success!

Help @kennyTE spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...