getting DOM element from absolute coordinates

W

wolverine

Hi,
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?

Thanks in Advance,
Kiran.
 
T

tomtom.wozniak

Hi,
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?

Thanks in Advance,
Kiran.

<div id="Foo">Foo</div>
<div id="Bar">Bar</div>
<script>
var len = document.getElementsByTagName('div').length;
for( var i = 0; i < len; i++ ) {
var obj = document.getElementsByTagName('div');
var x = 0;
var y = 0;
if (document.getBoxObjectFor) {
var bof = document.getBoxObjectFor(obj);
x = bof.x;
y = bof.y;
}
else {
var bcr = obj.getBoundingClientRect();
x = bcr.left;
y = bcr.top;
}
alert( obj.id + " - " + x + " : " + y );
}
</script>

Does this get you going?

Cheers.

-Tom Woz
 
D

David Mark

Hi,
     I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?
Thanks in Advance,
Kiran.

<div id="Foo">Foo</div>
<div id="Bar">Bar</div>
<script>
  var len = document.getElementsByTagName('div').length;
  for( var i = 0; i < len; i++ ) {
    var obj = document.getElementsByTagName('div');
    var x = 0;
    var y = 0;
    if (document.getBoxObjectFor) {
      var bof = document.getBoxObjectFor(obj);
      x = bof.x;
      y = bof.y;
    }


See the recent thread about finding an element's absolute position.
The gBOF function is taboo on the Web, according to Mozilla.
    else {
      var bcr = obj.getBoundingClientRect();

What makes you think this method is available? Perhaps you are
inferring it from the lack of gBOF? Object inferences are a bad idea
and this one is particularly ill-advised.

[snip]
 
S

SAM

(e-mail address removed) a écrit :
The code you have sent is absolutely marvelous
and works fine in my Fx 2.
I didn't know these 'getBoxObjectFor' and 'getBoundingClientRect'.

'getBoxObjectFor' seems to be a XUL feature

But ... and for others navigators ?
Would you have an idea about Safari, Opera, iCab ?
(other than what in bottom here)

<div id="Foo">Foo</div>
<div id="Bar">Bar</div>
<script>
var len = document.getElementsByTagName('div').length;
for( var i = 0; i < len; i++ ) {
var obj = document.getElementsByTagName('div');
var x = 0;
var y = 0;
if (document.getBoxObjectFor) {
var bof = document.getBoxObjectFor(obj);
x = bof.x;
y = bof.y;
}
else {
var bcr = obj.getBoundingClientRect();
x = bcr.left;
y = bcr.top;
}
alert( obj.id + " - " + x + " : " + y );
}
</script>



Found an answer here :
<http://www.quirksmode.org/js/findpos.html>
with comments about how react some browsers with the function :

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return [curleft,curtop];
}
 
D

David Mark

(e-mail address removed) a écrit :


The code you have sent is absolutely marvelous
and works fine in my Fx 2.
I didn't know these 'getBoxObjectFor' and 'getBoundingClientRect'.

'getBoxObjectFor' seems to be a XUL feature

Right. It has some odd quirks too. As does gBCR in IE.
But ... and for others navigators ?
Would you have an idea about Safari, Opera, iCab ?

All would have exceptions with the posted snippet.
(other than what in bottom here)




<div id="Foo">Foo</div>
<div id="Bar">Bar</div>
<script>
  var len = document.getElementsByTagName('div').length;
  for( var i = 0; i < len; i++ ) {
    var obj = document.getElementsByTagName('div');
    var x = 0;
    var y = 0;
    if (document.getBoxObjectFor) {
      var bof = document.getBoxObjectFor(obj);
      x = bof.x;
      y = bof.y;
    }
    else {
      var bcr = obj.getBoundingClientRect();
      x = bcr.left;
      y = bcr.top;
    }
    alert( obj.id + " - " + x + " : " + y );
  }
</script>


Found an answer here :
<http://www.quirksmode.org/js/findpos.html>
with comments about how react some browsers with the function :

function findPos(obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
                do {
                        curleft += obj.offsetLeft;
                        curtop += obj.offsetTop;
                } while (obj = obj.offsetParent);
        }
        return [curleft,curtop];

}


*That* was on quirksmode.org? See the other thread I mentioned.
Suffice to say, this is usable only in under a very narrow set of
circumstances.
 
S

SAM

David Mark a écrit :
Found an answer here :
<http://www.quirksmode.org/js/findpos.html>
with comments about how react some browsers with the function :

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return [curleft,curtop];

}

*That* was on quirksmode.org?

Yes. In code of given url.
See the other thread I mentioned.

Do you mean :
<http://groups.google.com/group/comp...5bed2100a9/e87a36385583cf93?#e87a36385583cf93>

Where is its digest ? ;-)
Suffice to say, this is usable only in under a very narrow set of
circumstances.

As I didn't find the finalized function's code I can't judge or test.
 
W

wolverine

Hi,
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?

Thanks in Advance,
Kiran.

Thanks for your reply. But i am looking for how to find element from
it's coordinates and NOT finding coordinates given an element. To be
more clear, i am looking for an equivalent of
document.elementFromPoint(x, y) which will work in FireFox
 
T

Thomas 'PointedEars' Lahn

wolverine said:
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?
[...]

Thanks for your reply. But i am looking for how to find element from
it's coordinates and NOT finding coordinates given an element. To be
more clear, i am looking for an equivalent of
document.elementFromPoint(x, y) which will work in FireFox

It is pretty much the same thing, only that you would have to find out the
absolute position of every element and compare with the given coordinates.

The question is: Why do you think you need that?


PointedEars
 
D

dhtml

wolverine said:
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?
[...]
Thanks for your reply. But i am looking for how to find element from
it's coordinates and NOT finding coordinates given an element. To be
more clear, i am looking for an equivalent of
document.elementFromPoint(x, y) which will work in FireFox

It is pretty much the same thing, only that you would have to find out the
absolute position of every element and compare with the given coordinates.
You could possibly simulate an event. That would be painful.
The question is: Why do you think you need that?
Yeah, why?
 
W

wolverine

wolverine said:
I would like to know if there is function to get the dom element
at any absolute coordinate. I know for IE there is a function. But
that will not work in other browsers like firefox. So do we have any
cross platform function which will get the dom element at any absolute
coordinate ?
[...]
Thanks for your reply. But i am looking for how to find element from
it's coordinates and NOT finding coordinates given an element. To be
more clear, i am looking for an equivalent of
document.elementFromPoint(x, y) which will work in FireFox

It is pretty much the same thing, only that you would have to find out the
absolute position of every element and compare with the given coordinates.

Thanks for spending your valuable time in reading and trying to answer
my question. Finding the absolute position of every element and then
comparing with given coordinates would be too costly if the web page
has a huge dom. The solution is almost like saying "i would walk 10km
daily to reach there instead of swimming 1km because i don't know to
swim."
The question is: Why do you think you need that?

PointedEars

I have a detect drag & drops (and some other things also) inside a web
page viewed in a browser. I inject javascript into web browser using
COM. Then i attach 'onmousedown/mousemove/onmouseup' to document and
on 'mouseup' i will get the (x,y) of the mouse. I also want to get
information about the element at (x,y). So i need the equivalent of
document.elementFromPoint(x, y).

Let it be clear that i don't write the web page. It can be written by
any web developer. Once again thanks for the reply.
 
T

Thomas 'PointedEars' Lahn

dhtml said:
wolverine said:
[...] But i am looking for how to find element from it's coordinates
and NOT finding coordinates given an element. To be more clear, i am
looking for an equivalent of document.elementFromPoint(x, y) which
will work in FireFox
It is pretty much the same thing, only that you would have to find out
the absolute position of every element and compare with the given
coordinates.

You could possibly simulate an event. That would be painful.

More important, it would certainly not help. A mouse event would be
required and there could be any number of event listeners already assigned
for that to the target element. For example, if I were to generate a
`mouseover' event, it could be that the element at the designated
coordinates handles that event and changes its position in the process.


PointedEars
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top