Mouse position
Pointer coordinates for mouse, pen and touch, with viewport, page and element values side by side, written at most once per frame.
CL-0046 fixesChecked 2026-10-151. The prompt
Not yet run against a named assistant · 88 words · CC-BY-4.0Write plain JavaScript that shows the pointer position over one box. Listen for pointermove on that box, not mousemove on the document, so pen and touch work too, and set touch-action: none on the box only. Show the pointer type and three rounded coordinate pairs: clientX and clientY, pageX and pageY, offsetX and offsetY. Keep only the newest event and write the readout at most once per animation frame with requestAnimationFrame. Write with textContent, never innerHTML, show a short message on pointerleave, and add an MIT licence comment.
What a one-line prompt gets you
Representative output for “show the mouse position in JavaScript” and nothing else, written by the editors to show the pattern. It runs. It also leaves these gaps.
- 1 It ignores pen and touch.It listens for mouse events only. MDN describes a pointer as a hardware-agnostic representation of a mouse, a pen or a touch contact, and PointerEvent inherits the same coordinates from MouseEvent.Prompt line: “Listen for pointermove on that box, not mousemove on the document.”
- 2 It reports page coordinates only.pageX counts from the left edge of the whole document, so it includes scrolling; a fixed tooltip needs clientX and a canvas usually needs offsetX. The reviewed readout shows all three pairs so you can pick.Prompt line: “Show the pointer type and three rounded coordinate pairs.”
- 3 It replaces other scripts' handlers.Assigning document.onmousemove overwrites any handler another script set there. The reviewed code adds a listener to one element.Prompt line: “Listen for pointermove on that box, not mousemove on the document.”
- 4 It writes to the page on every event.Every move rewrites the DOM, however fast events arrive. The reviewed code keeps the newest event and writes once per animation frame.Prompt line: “Write the readout at most once per animation frame with requestAnimationFrame.”
- 5 It writes with innerHTML.The readout is parsed as HTML. The reviewed code uses textContent, so nothing it writes is treated as markup.Prompt line: “Write with textContent, never innerHTML.”
- 6 It carries no licence.Nothing records who wrote it or on what terms. The reviewed script starts with an MIT licence line and a credit.Prompt line: “Add an MIT licence comment.”
Install it
Questions and answers
Has this prompt been tested with an assistant?
Not yet. The editors wrote the prompt to describe the reviewed code, which they wrote and tested with AI assistance and re-ran in a scripted DOM test on 2026-10-15. Nobody has run the prompt against a named assistant, so compare any answer with the code above.
Should I use clientX or pageX?
Use clientX for anything positioned against the viewport, such as a fixed tooltip, and pageX for elements placed in the document. MDN says clicking the left edge of the viewport always gives clientX 0 however far the page is scrolled, while pageX includes the scrolled part: 200 pixels scrolled away plus 100 pixels in gives 300.
Why does offsetX jump over a child element?
MDN defines offsetX from the padding edge of the event's target node, and the target changes when the pointer crosses into a child. For a stable position inside a container, subtract the container's getBoundingClientRect() left and top from clientX and clientY.
Why set touch-action: none on the box?
MDN's touch-action page says panning and pinching are handled by the browser by default, and a page using pointer events receives pointercancel when the browser takes over a gesture. The same page warns that touch-action: none may stop zooming, which low-vision readers rely on, so keep it on the box only.
Do I still need mousemove as well as pointermove?
No. PointerEvent inherits from MouseEvent, so pointermove carries the same coordinates and also fires for pen and touch. MDN notes that browsers often send both pointer and mouse events, for older code.
How do I know which device moved?
Read the event's pointerType property. MDN lists it as the device type that caused the event, such as mouse, pen or touch, and the reviewed readout prints it first.
Why are the coordinates decimals?
MDN records that pageX was originally an integer and was redefined as a double-precision value to allow subpixel precision; clientX and offsetX are doubles too. The script rounds them before printing.
Can I get the mouse position without an event?
Not directly; the browser reports the position inside events. Store the coordinates from the latest pointermove in a variable and read that variable when you need it, which is what the reviewed code does with the last event.
Can I use the script in a commercial project?
Yes. It is MIT licensed: keep the licence line or credit CodeLifter in your notices, and you can use, change and sell work that includes it.
