Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't create independent variables?

Status
Not open for further replies.

jemminger

Programmer
Jun 25, 2001
3,453
0
0
US
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=&quot;button&quot;
value=&quot;getCoords();&quot;
onclick=&quot;c = getCoords(this); alert(c.x + ',' + c.y)&quot; />
</td>
</tr>
</table>
</form>
</body>

the problem is that in getCoords(), if you uncomment the line &quot;//window.coords = null;&quot;, then it destroys the vars xx and yy inside tmp!!!

what am i doing wrong?



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
nevermind...ended up solving it a different way!



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
x and y are (sub)properties of the window property coords. When you set window.coords = null you do indeed distroy any properties it had. You might have wanted this...

window.coords.x = null;
window.coords.y = null;

Good Luck! [thumbsup2]
WindUp
 
what i was trying to do is store those two values in new vars, and destroy the original object...how can i do this?

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top