SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : Dynamic HTML :

Previous DiscussionNext Discussion
 How can I find location of an object in page?

When the user clicks a button, I'd like to be able to send the ID of its container object (e.g., a table) to (presumably) a Javascript routine to find out the top and left pixel coordinates of the object -- relative to the screen and/or browser window. Anyone know whether this can be done and how to go about it?

To trigger the same process, is it possible to capture the click event of the browser's scroll bars?

Started By scwilson on Oct 20, 2004 at 3:53:44 PM

13 Response(s) | Reply

Earlier Replies | Replies 1 to 6 of 13 | Later Replies
Goto Page: 2 1
ChrisRickard on Oct 20, 2004 at 4:39:26 PM (# 1)

Well to get the ID of the parent you first need a reference to the element that generated the event. Then loop up using the parentNode property until you find the right element. You can use its id property from there.

The ways of getting the top and left coordinates of the element for IE and Moz are as such

IE: element.getBoundingClientRect()

Mozilla: document.getBoxObjectFor() (I don't know why the hell Mozilla's DOM reference doesn't list this! It took me forever to find the method name because I thought it was document.getBoxModelFor)

 


hedger on Oct 20, 2004 at 7:32:26 PM (# 2)
This message has been edited.

Oh thanks God and dear ChrisRickard .

document.getBoxObjectFor()  is exactlly what I need.

Where did you learn that?

here is the general solution

function getPos(el) {
var x,w,y,h;
if (document.getBoxObjectFor) {
var bo = document.getBoxObjectFor(el);
x = bo.x;
w = bo.width;
y = bo.y;
h = bo.height;
}
else if (el.getBoundingClientRect) {
var rect = el.getBoundingClientRect();
x = rect.left;
w = rect.right - rect.left;
y = rect.top;
h = rect.bottom - rect.top;

el.x = x;
el.y=y;
el.w= w;
el.h = h;
alert("Left: " + x + "\rTop: " + y + "\rWidth: " + w + "\rHeight: " + h);
}


 


hedger on Oct 20, 2004 at 11:35:29 PM (# 3)

Google give you only :
Results 1 - 6 of about 11 for document.getBoxObjectFor. (0.28 seconds) 

only 6 results, ChrisRickard , did you get that by luck?(hehe)

 


ChrisRickard on Oct 21, 2004 at 8:10:20 AM (# 4)

I think I got it from poking around WebFx, but I'm not sure. It's certainly too important to be such an obscure method. I don't know why nobody ever mentions it...


hedger on Oct 21, 2004 at 8:48:52 AM (# 5)

One main reason may be that this is not a stabdard DOM method.
But for XUL, it is.
http://softwant.com/cgi-bin/kimsboard/mozilla/xultu/elemref/ref_XULElement.html

Here is the related article:

boxObject

Type: nsIBoxObject

This property is available for elements that are derived from boxes, which is most displayable XUL elements. You can retrieve the boxObject for non-XUL elements using the document.getBoxObjectFor method. The boxObject contains a number of properties, described below.

 

Note that it also provide some interesting methods, such as
getElementsByAttribute ( attrib , value )

He, he. We do have more to learn.

 

 


scwilson on Oct 22, 2004 at 5:56:47 AM (# 6)
This message has been edited.

Thanks to ChrisRickard  and hedger for all the help; glad also to see that the topic also useful to others

Steve (scwilson)

 


Earlier Replies | Replies 1 to 6 of 13 | Later Replies
Goto Page: 2 1

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2004 InsideDHTML.com, LLC. All rights reserved.