Getting the position of a mouse when its clicked

  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 09, 2008
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England
  • Status: Offline

Post October 6th, 2009, 1:08 am

What I want to do is get the x,y (or its it top and right im not sure) position of the mouse when it is click. Then saving it to a variable so I can add it to mysql im sure it can be done in js but not sure where to start.
^__^
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 6th, 2009, 1:08 am

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post October 6th, 2009, 7:32 am

Code: [ Select ]
<html>
    <head>
        <script type="text/javascript">
            
            window.onload = init;
            function init() {
                if (window.Event) {
                    document.captureEvents(Event.MOUSEMOVE);
                }
                document.onmousedown = getPosition;
            }

            function getPosition(e) {
                xPos = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                yPos = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                
                alert("X: " + xPos + " Y: " + yPos);
            }
            
        </script>
    </head>
    <body>
            
    </body>
</html>
  1. <html>
  2.     <head>
  3.         <script type="text/javascript">
  4.             
  5.             window.onload = init;
  6.             function init() {
  7.                 if (window.Event) {
  8.                     document.captureEvents(Event.MOUSEMOVE);
  9.                 }
  10.                 document.onmousedown = getPosition;
  11.             }
  12.             function getPosition(e) {
  13.                 xPos = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  14.                 yPos = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  15.                 
  16.                 alert("X: " + xPos + " Y: " + yPos);
  17.             }
  18.             
  19.         </script>
  20.     </head>
  21.     <body>
  22.             
  23.     </body>
  24. </html>


Tested in FF3.5 and IE7. This gets you the position, but there's a few different ways to go about persisting those values to a DB, so that depends on your needs. If you need it to be snappy, then you could wire up an ajax call to your favorite SS language and save it that way.
I'd love to change the world, but they won't give me the source code.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: Kurthead+1, ScottG and 99 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.