Forum Moderators: open

Message Too Old, No Replies

how to find the position of coordinate on clicked position?

         

PHPycho

10:21 am on May 21, 2007 (gmt 0)

10+ Year Member



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

colandy

10:48 am on May 21, 2007 (gmt 0)

10+ Year Member



in IE:

<img src="....." onClick="getClick(event)">

in FF:

<img id="img" src="......" onClick="javascript:getClick1()">

IE Javascript:
function getClick(event)
{
xcoord=event.clientX;
ycoord=event.clientY;
}

FF Javascript:

var xcoord;
var ycoord;

function getCoords(e)
{
xcoord=e.clientX;
ycoord=e.clientY;
}

function getClick1()
{
.........
}

window.onload=function(){
document.getElementById('img').addEventListener('C lick',getCoords,false);
}

-----------------------------------------------

Unfortunately FF requires an event listener to capture mouse events, whereas IE has one built in. Hopefully you get ther idea, I know there are better ways but this should point you in the right direction.

PHPycho

5:24 am on May 30, 2007 (gmt 0)

10+ Year Member



knock knock!
but the clicked coordinates for the same position are different for different resolutions..How to avoid such problems...
thanks in advance to all of you
Awaiting for the valueable help

colandy

9:05 am on May 30, 2007 (gmt 0)

10+ Year Member



The code mentioned above will tell you the exact co-ords of the mouse on the screen when the left click button was pressed, why does the resolution of the screen make any diff.

If the screen res is 800x600 then u won't get and co-ords greater than 800x600. If the res is higher then the co-ords returned will reflect this.

Not sure why the screen res should make any difference to the location of you click, the only diff being the coords returned.

daveVk

12:58 pm on May 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume PHPycho is saying size of the image in pixels changes with screen resoluton? If this is so, the coordinates you want are mouseX/image.offsetWidth and mouseY/image.offsetHeight.