Friday, July 3, 2009

How To Find Position Of An HTML element

function GetPos(Cord,Obj) {
var curPos = 0;
try {
switch(Cord)
{
case 'X':
case 'Y':
if (Obj.offsetParent)
{
while (1)
{
if(Cord == 'X'){
curPos += Obj.offsetLeft;
}else{
curPos += Obj.offsetTop;
}
if (!Obj.offsetParent) break; Obj = Obj.offsetParent;
}
}
else
{
if(Cord == 'X'){
if (Obj.x)curPos += Obj.x;
}
else{
if(Obj.y)curPos += Obj.y;
}
}
break;
}
}
catch (e) {
document.write('An exception occurred.');
}
finally {
return curPos;
}
}

function callfn(Cord, func, obj) {

return func(Cord, obj);
}

//way of Calling Function
"alert(callfn('X',GetPos,document.getElementById('elementId')))" // X co-ordinates
"alert(callfn('Y',GetPos,document.getElementById('elementId')))" // Y co-ordinates



No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.