faqts : Computers : Programming : Languages : JavaScript : Images

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

42 of 46 people (91%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Can I check the position (x,y) of an IMG when clicked?

Aug 29th, 2000 09:51
Martin Honnen, kreangsak jiwattayakul,


Here is some code that shows an alert if an IMG is clicked with the 
position of the IMG. It works with NN4, IE4+ and NN6:
<HTML>
<HEAD>
<SCRIPT>
function clickHandler (evt) {
  if (document.layers) {
    if (evt.target.constructor == Image)
      alert('Image at position ' + evt.target.x + ':' + evt.target.y 
+ ' clicked.');
    return true;
  }
  else if (document.all) {
    if (event.srcElement.tagName == 'IMG')
      alert('Image at position ' + event.srcElement.offsetLeft + ':' + 
event.srcElement.offsetTop + ' clicked.');
    return true;
  }
  else if (document.getElementById) {
    if (evt.target.tagName == 'IMG')
      alert('Image at position ' + evt.target.offsetLeft + ':' + 
evt.target.offsetTop + ' clicked.');
    return true;
  }
}
if (document.layers) {
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown = clickHandler;
}
else
  document.onclick = clickHandler;
</SCRIPT>
</HEAD>
<BODY>
Kibology for all.
<IMG NAME="anImage" SRC="kiboInside.gif">
</BODY>
</HTML>