this set of functions is intended to return the position of an element on the page:
[tt]
function getCoords(o) {
setCoords(o);
var xx = new Number(window.coords.x);
var yy = new Number(window.coords.y);
var tmp = {x:xx, y:yy};
//window.coords = null;
return tmp;
}
function setCoords(o) {
if (!window.coords)
window.coords = new Coords(o.offsetLeft, o.offsetTop);
else window.coords.add(o.offsetLeft, o.offsetTop);
if (o.tagName == "BODY" {
return window.coords;
}
else {
getCoords(o.offsetParent);
}
}
function Coords(x,y) {
this.x = x;
this.y = y;
return this;
}
Coords.prototype.add = function(x,y) {
this.x += x;
this.y += y;
}
[/tt]
usage:
<body>
<form>
<table>
<tr>
<td>
<input type="button"
value="getCoords();"
onclick="c = getCoords(this); alert(c.x + ',' + c.y)" />
</td>
</tr>
</table>
</form>
</body>
the problem is that in getCoords(), if you uncomment the line "//window.coords = null;", then it destroys the vars xx and yy inside tmp!!!
what am i doing wrong?
=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
[tt]
function getCoords(o) {
setCoords(o);
var xx = new Number(window.coords.x);
var yy = new Number(window.coords.y);
var tmp = {x:xx, y:yy};
//window.coords = null;
return tmp;
}
function setCoords(o) {
if (!window.coords)
window.coords = new Coords(o.offsetLeft, o.offsetTop);
else window.coords.add(o.offsetLeft, o.offsetTop);
if (o.tagName == "BODY" {
return window.coords;
}
else {
getCoords(o.offsetParent);
}
}
function Coords(x,y) {
this.x = x;
this.y = y;
return this;
}
Coords.prototype.add = function(x,y) {
this.x += x;
this.y += y;
}
[/tt]
usage:
<body>
<form>
<table>
<tr>
<td>
<input type="button"
value="getCoords();"
onclick="c = getCoords(this); alert(c.x + ',' + c.y)" />
</td>
</tr>
</table>
</form>
</body>
the problem is that in getCoords(), if you uncomment the line "//window.coords = null;", then it destroys the vars xx and yy inside tmp!!!
what am i doing wrong?
=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff