get XY coords of click on image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stimp

    get XY coords of click on image

    I remember doing something like this before, but it's been a while since
    I've touched javascript...

    I have a standard <img> and when a user clicks the image I want
    to output the IMAGE x y coordinates of the mouse click (i.e. not
    the page x y coords).

    I know that appending 'ismap' to the image tag will give me the image
    coordinates, but I can't use that since the img already has a
    'usemap' attribute.

    I did some searching on google and came up with the following code
    which only seems to return the page XY coordinates:

    <script type="text/javascript">
    <!--
    var getX = function(evt){
    if(evt.x){ return evt.x; }
    if(evt.pageX){ return evt.pageX; }
    }
    var getY = function(evt){
    if(evt.y){ return evt.y; }
    if(evt.pageY){ return evt.pageY; }
    }
    var alertCoords = function(evt){
    alert("X = "+ getX(evt) +"\nY = "+ getY(evt));
    }
    // -->
    </script>
    </head>
    <body>
    <div onclick="alertC oords(event);"> Click ME</div>
    </body>
    </html>

    The code I'm looking for should be something similar!
    Thanks :)


    --

    "I hear ma train a comin'
    .... hear freedom comin"
  • Java  script  Dude

    #2
    Re: get XY coords of click on image

    Some code from my libraries:

    // Your client sniffer
    is=new ClientSniffer()// Should set is.ie=true if IE & is.moz=true if
    Mozilla...
    // Abbrev Vars
    _w=window
    _d=document
    // X & Y Code
    function getEventX(e){
    return is.ie?e.clientX +_d.body.scroll Left:e.clientX+ _w.pageXOffset
    }
    function getEventY(e){
    return is.ie?e.clientY +_d.body.scroll Top:e.clientY+_ w.pageYOffset
    }

    This code will work with both Mozilla and IE. It's not tested with any
    other browsers.

    JsD

    Comment

    • Randy Webb

      #3
      Re: get XY coords of click on image

      Java script Dude wrote:
      [color=blue]
      > Some code from my libraries:[/color]

      If its indicative of all your libraries, users should beware of it.
      [color=blue]
      > // Your client sniffer
      > is=new ClientSniffer()// Should set is.ie=true if IE & is.moz=true if
      > Mozilla...[/color]

      Truely cross-browser scripts dont care what browser. It tests for
      supported features, not browser names.
      [color=blue]
      > // Abbrev Vars
      > _w=window
      > _d=document
      > // X & Y Code
      > function getEventX(e){
      > return is.ie?e.clientX +_d.body.scroll Left:e.clientX+ _w.pageXOffset
      > }
      > function getEventY(e){
      > return is.ie?e.clientY +_d.body.scroll Top:e.clientY+_ w.pageYOffset
      > }
      >
      > This code will work with both Mozilla and IE. It's not tested with any
      > other browsers.[/color]

      But you did not include the ClientSniffer() function, so it doesn't work.

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      Working...