P
Pugi!
I create elements dynamically on a webpage using AJAX. I add events
(like onclick). Everything works fine I can retrace the origin of the
event, the parentnode, ... but now I have to put a layer (a form) on
the image from which the event originated. I have foun some code on the
web that works fine for FF and Opera, but not for IE (6 & 7). The
top-position is good, but not the left (more then 100px to the left of
for IE). I have found another method that gets the absolute x-position
of an element but works only good for IE 6 & 7. How do I distinguish
between browsers ? Normally you test wether an object exists like if
(document.all) ... Problem: Opera also knows how to handle
document.all. For the moment I use this:
function getXYCoordinates(obj) {
var curleft = curtop = 0;
var x = obj.offsetLeft; // for IE
if (obj.offsetParent) {
curleft = obj.offsetLeft;
curtop = obj.offsetTop;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}
This works for FF and IE 6 & 7 but not for Opera. If I skip the test
(document.all) and return [curleft, curtop] it works fine for FF and
Opera but not for IE 6 & 7.
How can I test an relevant object to determine wether I should return
[x, curtop] instead of [curleft, curtop] ?
Or is there a better method to determine the position of an object that
raised an event.
thanx,
Pugi!
(like onclick). Everything works fine I can retrace the origin of the
event, the parentnode, ... but now I have to put a layer (a form) on
the image from which the event originated. I have foun some code on the
web that works fine for FF and Opera, but not for IE (6 & 7). The
top-position is good, but not the left (more then 100px to the left of
for IE). I have found another method that gets the absolute x-position
of an element but works only good for IE 6 & 7. How do I distinguish
between browsers ? Normally you test wether an object exists like if
(document.all) ... Problem: Opera also knows how to handle
document.all. For the moment I use this:
function getXYCoordinates(obj) {
var curleft = curtop = 0;
var x = obj.offsetLeft; // for IE
if (obj.offsetParent) {
curleft = obj.offsetLeft;
curtop = obj.offsetTop;
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
}
if (document.all) { // here I should test for IE, but also Opera gets
caught
return [x,curtop];
} else {
return [curleft,curtop];
}
}
This works for FF and IE 6 & 7 but not for Opera. If I skip the test
(document.all) and return [curleft, curtop] it works fine for FF and
Opera but not for IE 6 & 7.
How can I test an relevant object to determine wether I should return
[x, curtop] instead of [curleft, curtop] ?
Or is there a better method to determine the position of an object that
raised an event.
thanx,
Pugi!