4

i am making a bookmarklet in the bookmarklet i am adding a iframe to the page's body.

i need to find the current mouse position to position the iframe accordingly.

The issue is. i cannot bind on click for the bookmarklet because by the time the script within the href="javascript:" runs the click was already done. so i cannot get the event there.

i thought of binding mousemove, but that's a ugly workaround.

so is there a way to get mouse's current x/y ? without an event firing ?

Thanks.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
  • Check the accepted answer on [this question](http://stackoverflow.com/questions/4517198/how-to-get-mouse-position-in-jquery-without-mouse-events). Is that helping you out? – Mandy Schoep Jan 18 '14 at 14:08
  • Can't you put the code of getting the position in your `href="javascript:"` function? – Mandy Schoep Jan 18 '14 at 14:10
  • No there's no way to get mouse position without triggering one of the [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent). – Givi Jan 18 '14 at 14:15
  • Mouse up is a no go, we are way past click event when the script executes. @roy how? – Neta Meta Jan 18 '14 at 14:17
  • If you give me the information whats in the `href="javascript:"` call I can make something up for you. – Mandy Schoep Jan 18 '14 at 14:38
  • its a `function (function(){ if(!window.myfun){(function(){window.myfun = true});(function(){ ........ everything else ....}) }})` something like that – Neta Meta Jan 18 '14 at 14:46
  • "i thought of binding mousemove, but that's a ugly workaround." Why? Look at [jsFiddle](http://jsfiddle.net/GKDev/vsL6k/1/) – Givi Jan 18 '14 at 14:48
  • the reason i dont want to use mouse move is because if i bind mouse move and the user chose to stick in the position then it wont work, and i need the position only 1 time at the time of the click, so i will have to set a certain check etc..its not clean solution but i guess i'll have to use mousemove – Neta Meta Jan 18 '14 at 15:01
  • Your question is a bit unclear. Why you can't add another click event handler if you want to get mouse positions only once? An element can have as much event handlers as you wish. – Givi Jan 18 '14 at 15:08
  • Do you know what bookmarklets are ? those are javascript snippet codes that you can add to your action bar, ones clicked they will perform a certain script. the idea is ones a bookmarklet is clicked, open a window with details and position that window near the bookmarklet. – Neta Meta Jan 18 '14 at 15:18
  • 2
    Even if there were a way to get mouse cursor position without an event firing (almost positive there isn't), the cursor will be outside of the viewport anyway when the user clicks a bookmarklet, and events that report mouse position don't fire when the cursor is outside of the viewport. IOW mouse positions outside of the viewport are never reported, so even without events, you're pretty much out of luck. – Dagg Nabbit Jan 18 '14 at 18:42
  • Remember that you can only get mouse position inside body, You can never get windows mouse position. – James Jan 18 '14 at 19:26
  • what i will end up doing is bind on mousemove to document. set a "global" variable that is being updated, with the position, then have an interval running to check the mouse position, ones that found, i will kill the interval and kill the bind. – Neta Meta Jan 18 '14 at 19:29
  • Possible duplicate of [How to get the mouse position without events (without moving the mouse)?](http://stackoverflow.com/questions/2601097/how-to-get-the-mouse-position-without-events-without-moving-the-mouse) – ericsoco Aug 03 '16 at 06:26

2 Answers2

2

Javascript is a language to add client side functionality on user interaction. Therefore, a javascript code snippet is always run as response to an event.

I would suggest using the onmousemove event. The event would be raised each time the cursor mouse would change. Therefore, you will know the mouse position at all times.

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36
0

Have you tried something like setTimeOut()? As far as I can tell you, there is no way to see the mouse position without using an Event object to do that.

Please, take a look at this thread:

Javascript - Track mouse position

Community
  • 1
  • 1
D. Melo
  • 2,139
  • 2
  • 14
  • 19