admin管理员组

文章数量:1389204

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.

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.

Share Improve this question edited Jan 18, 2014 at 14:53 danronmoon 3,8735 gold badges35 silver badges58 bronze badges asked Jan 18, 2014 at 13:50 Neta MetaNeta Meta 4,03710 gold badges47 silver badges71 bronze badges 14
  • Check the accepted answer on this question. Is that helping you out? – Mandy Schoep Commented Jan 18, 2014 at 14:08
  • Can't you put the code of getting the position in your href="javascript:" function? – Mandy Schoep Commented Jan 18, 2014 at 14:10
  • No there's no way to get mouse position without triggering one of the MouseEvent. – Givi Commented Jan 18, 2014 at 14:15
  • Mouse up is a no go, we are way past click event when the script executes. @roy how? – Neta Meta Commented Jan 18, 2014 at 14:17
  • 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 Commented Jan 18, 2014 at 18:42
 |  Show 9 more ments

2 Answers 2

Reset to default 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!

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

本文标签: javascriptis there a way to find mouse current position on the screen without an eventStack Overflow