Tuesday, August 21, 2012

Simple HTML/Javascript Codes to Display your Live Mouse Coordinates (Firefox Fix)

The firefox friendly version:


<html>
<head>

<script type="text/javascript">

var ff = (navigator.userAgent.indexOf("Firefox") != -1);

function showXY(e)
{
  if (ff) {
     var s = 'X=' + e.pageX + ' Y=' + e.pageY;
     document.getElementById('coordinates').textContent = s;
  }
  else {
     var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY;
     document.getElementById('coordinates').innerText = s;
  }
}

document.onmousemove = showXY;

</script>
</head>

<body>

<div id="coordinates"></div>

</body>
</html>


Here's the firefox friendly version. Instead of using onmousemove event capture in the body tag, it is initiated in javascript before the body. IF you initiate the event in the body tag, it won't work. A rare Firefox problem with a weird solution. But it works.

No comments:

Post a Comment