Y-Position of Element on Web Page

L

Lit

Hi,

How can I get the Y-Position ( Vertical ) of an Control via JavaScript from
the top of the Window.

I have tried the following script but no luck I keep on getting 15

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}



I don't understand why, I keep on getting 15 for an element in the middle of
the page that is 900 pixels down on the page.

The loop is going up the parents many times but the total answer is 15. I
have tried similar script that I found on the Internet but getting the same
result. is this due to some elements have no ID?

Thank you,

Lit
 
M

Muhammad Naveed Yaseen

Following returns y position as integer in pixels relative to nearest
absolute position container (which usually is client area of browser)

function findPosY(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
 
M

Muhammad Naveed Yaseen

You may have some parent in hierarchy which has 'position' style set
to 'fixed' or 'absolute' which would mess up OffsetTop aggregation.
 
L

Lit

Muhammad,

To my Surprise I had the script function at the bottom of the page and the
function only works from an Event, ( for testing I just dropped the
JavaScript loose) this could be due to render-completion issues, also the
Border size of elements and Font Size when Images and text are under the
same element can mess things up.

the reason I needed the y-position is because I was trying to find a
solution to SetFocusOnError for Validation to set the focus on the element
with the error -70 pixels because it was too close to the top and does not
look good.

Thank you for any further input in this matter,

Lit
 
M

Muhammad Naveed Yaseen

If it is just an offset scroll relative to current position you may
try window.scrollBy(0, -70)
 
L

Lit

I am doing that, but where to hook into the end of a validation
"SetFocusOnError" in ASP.NET 2.0 is unknown. I am currently doing all my
validation using Custom Validation to deal with this.

I wish Microsoft can allow us too hook into After Validation event. Or
better yet After the SetFocusOnError I can append and run a script of my
own.

Thank you,

Lit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top