The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Mouse x,y coords in Javascript?
Discuss Mouse x,y coords in Javascript? in the JavaScript Development forum on Dev Shed. Mouse x,y coords in Javascript? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 25th, 2001, 08:33 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 130
Time spent in forums: 25 m 5 sec
Reputation Power: 13
|
|
Mouse x,y coords in Javascript?
Is there a way to retrieve the x,y coordinates of the mouse in Javascript? I'm not looking for a coodinate stream, just the x,y onclick(). It's going to be my target for a DHTML flyout.
|

June 25th, 2001, 09:05 AM
|
|
Contributing User
|
|
Join Date: May 2001
Location: The Netherlands
Posts: 200
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|

June 25th, 2001, 11:00 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 130
Time spent in forums: 25 m 5 sec
Reputation Power: 13
|
|
|
Actually, you can do this in both IE and Netscape. The variables are:
IE:
window.event.clientX, and windows.event.clientY
Netscape:
e.pageX and e.pageY
|

June 25th, 2001, 07:10 PM
|
|
Junior Member
|
|
Join Date: Feb 2001
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Actually Void there is a way for NS, and Statik you're missing something.
<script language="javascript">
function getMousePos(e) { //you need the 'e', although it does NOT need to be defined
//NS
if (document.layers||document.getElementById&&!document.all) {
alert(e.pageX+"and"+e.pageY)
}
//IE
else if (document.all) {
form1.X.value=window.event.clientX
form1.Y.value=window.event.clientY
}
}
if(!window.captureEvents) document.onmousemove=getMousePos
if (window.captureEvents) {
window.captureEvents(Event.MOUSEMOVE)
window.onmousemove=getMousePos //e should not be defined
}
</script>
<body>
<form name="form1">
<input name="X" value="">
<input name="Y" value="">
</form>
</body>
This should do it! In NS you can't use the form, because you can't change the value of the form, for some reason. Hope it helps!
|

June 25th, 2001, 09:27 PM
|
|
Senior Citizen
|
|
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019
Time spent in forums: < 1 sec
Reputation Power: 15
|
|
function getMousePos(e) {
//NS
if (document.layers||document.getElementById&&!document.all) {
document.form1.X.value = e.pageX
document.form1.Y.value = e.pageY
}
//IE
else if (document.all) {
form1.X.value = event.clientX
form1.Y.value = event.clientY
}
}
Don't call alert()s in a loop!
Use status as an alternative.
e is the event object, passed automatically with property assignment (window.onmousemove=getMousePos); if you're calling handlers from HTML, gotta pass it yer own self...Netscape 6 supports pageX & Y properties, but also provides DOM standard clientX & Y as well.
http://www.brainjar.com/dhtml/events/default4.asp
cheers, adios
Last edited by adios : June 25th, 2001 at 10:03 PM.
|

June 26th, 2001, 09:15 AM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Toronto, Ontario, Canada
Posts: 631
Time spent in forums: 7 m 19 sec
Reputation Power: 12
|
|
*This is Blue Angel*
LOL, Adios, I didn't know how to access the forms... so all I could think of was to alert it. It's easy to get out of though. Put your mouse at the top of the screen and push enter 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|