PDA

View Full Version : how to find the position of coordinate on clicked position ?



PHPycho
05-21-2007, 11:55 AM
hello forums!!
I am wondering to know -
how to find the position of coordinate on clicked position ? using javascript.

Case:
Suppose i had a image ,when mouse is clicked @ certain position of image, then it should alert the x & y coordinate of that point..

Awaiting for your great help..
Thanks in advance to all of you

Trinithis
05-21-2007, 05:03 PM
var Xm, Ym;
if(window.addEventListener) window.addEventListener("click", mouseClick, false);
else window.document.attachEvent("onclick", mouseClick);
function mouseClick(e){
var o = (e.target) ? e.target : e.srcElement;
Xm = (e.pageX) ? e.pageX : e.clientX;
Ym = (e.pageX) ? e.pageY : e.clientY;
alert(o+"\nAbs: ("+Xm+","+Ym+")"+"\nRel: ("+(Xm-o.offsetLeft)+","+(Ym-o.offsetTop)+")");
}

rajug
05-22-2007, 04:01 AM
Try out this too:


document.onclick = function(e){
var curleft = curtop = 0;
var curleft1 = curtop1 = 0;

var e = e ? e : window.event;
var obj = e.target? e.target : e.srcElement;
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
alert('It\'s an anchor\r\nX = ' + curleft + '\r\nY=' + curtop);
}

djr33
05-22-2007, 04:25 AM
You could also use the image input element in a form and get the clicked cooridinates as a result of the submitted form if that fits your plan.

form:
<form action="next.php" method="post"...>
<input type="image" name="clickimg">
...
</form>

On next.php:
<?php
echo $_POST['clickimg_y'];
echo " - "; //just to seperate
echo $_POST['clickimg_x'];
?>