Wednesday, November 12, 2008

The correct script for detecting the mouse coordinates...

Hi...
Dear friend Now again I am here with a very good post which get you free from tension of calculating the co-ordinates(X and Y) of mouse clicking .

function doSomething(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}

And Also I have something more of calculating the C0-0rdinates(X and Y) of any HTML object (eg: Textbox or any other.) so I am giving you 2 javascript function look them...


function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
while(1)
{
curleft += obj.offsetLeft;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.x)
curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}







Enjoy scripting...
Sanjeev Kumar Chauhan
(On Behalf Of Help On Desk Team)

1 comment:

Mahesh K. Sharma said...

Yes its also working on grids :)