Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cursor X,Y coordinates - Fine in FireFox not in IE 1

Status
Not open for further replies.

gsyne

Programmer
May 21, 2009
2
CA
Hello, I grabbed this function below from a webpage that returns sets the X,Y coordinates to a variable. It works perfect in Firefox but not in IE - I should say it works in IE too, but not how I want it to.

In IE it returns the X,Y coordinates from your current viewing point on the page, meaning if you are scrolled halfway down the page and your cursor was top left of the screen it would return 0,0. When it should return 0,>0.

How do I fix this function to return the correct X,Y coordinates of the cursor regardless of your viewing point of the screen in IE?

Code:
var tempX = 0;
var tempY = 0;

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else {  // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}  
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}  

return true;
}
 
P.S. I'd rework your script as window.captureEvents is obsolete and unnecessary:

Code:
<script type="text/javascript">
	var tempX = 0;
	var tempY = 0;

	document.onmousemove = getMouseXY;

	function getMouseXY(e) {
		if (window.event && !e) {
			tempX = event.offsetX;
			tempY = event.offsetY;
		} else {
			tempX = e.pageX;
			tempY = e.pageY;
		}

		if (tempX < 0) tempX = 0;
		if (tempY < 0) tempY = 0;

		return true;
	}
</script>



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top