devxlogo

Determine Position of Object Relative to Page

Determine Position of Object Relative to Page

Question:

I am using the eventObj.x and eventObj.y to obtain the position of a link when the mouse is over it to determine where to show a help text box. In Netscape and Internet Explorer 4, the eventObj.y returns the number of pixels the object is from the top of the page. Therefore, if it were 50 pixels from the top, it stayed consistent no matter where I scrolled to on the page.

But in IE5, it was not working, so I used the alert box to return the value when the mouse moved over the object. If I scrolled on the page so the link object was at the top edge of the visible window, then it returned a low number (2, 3, and so on). If I scrolled down, the number increased. So the position where the text help box appeared changed depending on where the window was scrolled to, instead of appearing next to the link each time.

Is this a flaw in IE5, or does IE5 have another way to determine the x and y position relative to the page? In other words, in IE5, it appears that eventObj.y returns the number of pixels that the link is from the top of the visible window, not of the Web page itself.

Answer:

Navigator and IE both support x and y in relation to a page object, but its meaning and hence its value do differ between the two browsers. Both support x and y to mean an object’s position relative to the object’s container?and there’s where your differences can occur.

If you want to get a position for an object relative to the page, you need to use different properties based on browser type. If you want to capture the object’s left and top position (x and y) for IE, then I suggest you use the clientX and clientY values. If you want to capture the link’s left and top position for Navigator, then you should consider using pageX and pageY. The x and y values you are using are the x and y position of the object relative to the containing layer?they’re really layerX and layerY. If the link is within a positioned DIV block, you would not get an accurate position of the object relative to the page. If you use pageX and pageY, you should get values relative to the browser page.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist