Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: onmouseclick get click coordinates

  1. #1
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Exclamation onmouseclick get click coordinates

    hi guys, right i know you will know this one.

    I need to get the mouse coordinates onclick of a link. the link doesnt go anywhere, it opens up a div. but i need to make the divs position dynamic depending on where abouts the click was.

    il give an example

    <a href="#" onclick="ajaxFunction(this.id)" id="dog">dog</a>
    <a href="#" onclick="ajaxFunction(this.id)" id="cat">cat</a>
    <a href="#" onclick="ajaxFunction(this.id)" id="mouse">mouse</a>

    when one of these links gets pressed it displays a div that gets filled with results based on the id passed to the ajax.
    This div needs to be positioned based on the click as the user may have the history window open and so this may force it else where as it positioned absolutely.

    so reiterate, i need to get the mouse coordinates based on onclick and then i need to edit the divs position based on these coordinates

    any help please, im at a loss with javascript really
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can try doing something with:
    Code:
    event.clientX
    And
    Code:
    event.clientY
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Ok well i gathered that bit from searching. I just dont know how to get the coordinates from the click thats all. thats the bit i need.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Here's something old. Stll working fine:
    Code:
    <html>
    <head>
    <!--
    This file retrieved from the JS-Examples archives
    http://www.js-examples.com
    1000s of free ready to use scripts, tutorials, forums.
    Author: JS-Examples - http://www.js-examples.com/
    -->
    
    <script type=text/javascript>
    var isIE = document.all?true:false;
    if (!isIE) document.captureEvents(Event.CLICK);
    document.onclick = getMousePosition;
    function getMousePosition(e) {
      var _x;
      var _y;
      if (!isIE) {
        _x = e.pageX;
        _y = e.pageY;
      }
      if (isIE) {
        _x = event.clientX + document.body.scrollLeft;
        _y = event.clientY + document.body.scrollTop;
      }
    
    posX=_x;
    posY=_y;
    
      return true;
    }
    
    </script>
    
    </head>
    <body onclick=alert("X-position:&nbsp;"+posX+";&nbsp;Y-position:&nbsp;"+posY+".")>
    
    Click to track the mouse position.
    
    </body>
    </html>
    Arie Molendijk.

  5. The Following User Says Thank You to molendijk For This Useful Post:

    city_coder (04-21-2008)

  6. #5
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    You my friend are a good man! just out of curiosity, if you put that into an a onclick so:

    <a href="#" onclick=alert("X-position:&nbsp;"+posX+";&nbsp;Y-position:&nbsp;"+posY+".")>Click to track the mouse position.</a>

    it doesnt work for the first mouse click on the page, it will work every time after that though. just wondered why, im not that bothered like.

    cheers all the same
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  7. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by city_coder View Post
    it doesnt work for the first mouse click on the page, it will work every time after that though. just wondered why, im not that bothered like.
    Sorry, I made a mistake. Replace your page with this one:
    Code:
    <html>
    <head>
    
    <script type=text/javascript>
    var isIE = document.all?true:false;
    document.onmousemove = getMousePosition;
    function getMousePosition(e) {
      var _x;
      var _y;
      if (!isIE) {
        _x = e.pageX;
        _y = e.pageY;
      }
      if (isIE) {
        _x = event.clientX + document.body.scrollLeft;
        _y = event.clientY + document.body.scrollTop;
      }
    
    posX=_x;
    posY=_y;
    
      return true;
    }
    
    </script>
    
    </head>
    <body onclick=alert("X-position:&nbsp;"+posX+";&nbsp;Y-position:&nbsp;"+posY+".")>
    
    Click to track the mouse position.
    
    </body>
    </html>
    That will solve the problem.
    ----
    Arie M.
    Last edited by molendijk; 04-21-2008 at 04:58 PM. Reason: Correction

  8. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Can you explain to me what the e variable does? It doesn't look like your using it.
    Jeremy | jfein.net

  9. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by Nile View Post
    Can you explain to me what the e variable does? It doesn't look like your using it.
    You don't need it for IE, but you do need it for non-IE.

    IE:
    We have event.clientX + document.body.scrollLeft and event.clientY + document.body.scrollTop, which does the mousetracking by itself.

    Non-IE:
    We have somevariable.pageX and somevariable.pageY. The variable only gets its concrete value of we do a mouse-event. For that reason, we must have the variable in our function, here: getMousePosition(somevariable). I used 'e' for the variable, but I could have equally well have used 'f', in which case we should have:
    function getMousePosition(f){
    .....
    .....
    _x = f.pageX;
    _y = f.pageY;
    .....
    .....

    etc.

    That's it.
    -----
    Arie M.

  10. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    So, I don't need to have a value for that? I just need to define it?
    Jeremy | jfein.net

  11. #10
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    That's right,
    Arie.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •