htmldoctypemouse-coordinates

Javascript mouse coordinates and declaring DOCTYPE


I want to show mouse coordinates in a page and when I don't declare DOCTYPE it works but when I declare DOCTYPE it doesn't! Could you please help me with that? here is my code:

<html>
<head>
    <title>problem</title>
</head>
<body>
    text...
<div id="show"></div>
<script>
    document.body.onmousemove = function(event) {
        document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
    }
</script>
</body>
</html>

In the code above I can get y coordinates with no problem but when I add a doctype it doesn't show y coordinates correctly:

<DOCTYPE html>
<html>
<head>
    <title>problem</title>
</head>
<body>
    text...
<div id="show"></div>
<script>
    document.body.onmousemove = function(event) {
        document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
    }
</script>
</body>
</html>

EDIT

here is my code and it works perfectly now. Thank you all:

<!DOCTYPE html>
<html>
  <head>
    <title>problem</title>
  </head>
  <body>
    text...
    <div id="show"></div>
    <script>
    if (document.addEventListener) {
      document.addEventListener('mousemove', function(event) {
        document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
      });
    } else {
      document.attachEvent("onmousemove", function(event) {
        document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
      });

}
   </script>
  </body>
</html>

Solution

  • Try do listen mouse event with event Listener, like this:

    <!DOCTYPE html>
    <html>
      <head>
        <title>problem</title>
      </head>
      <body>
        text...
        <div id="show"></div>
        <script>
        document.addEventListener('mousemove', function(event) {
            document.body.innerHTML = "X: " + window.event.clientX + "<br />" + "Y: " + window.event.clientY;
        });
       </script>
      </body>
    </html>