Mouse on page…

If your name is Scott, you’ll know that i’ve been having issues during the past week with some javascript that would take the x,y coords of the mouse, on the screen and then redirect to a php script with the x,y in the GET.

Needless to say, I had issues. But I think I’ve cracked the nut now. What I have:


<script language="javascript" type="text/javascript">
<!-- Begin
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSECLICK)
document.onclick = getMouseXY;
function getMouseXY(e) {
var tempX = 0;
var tempY = 0;
var continue1 = true;
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.documentElement.scrollLeft;
tempY = event.clientY + document.documentElement.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
tempX = tempX-8;
if (tempX <0){continue1 = false;}
if (tempX > 1000){continue1 = false;}
tempY = tempY-129;
if (tempY <0){continue1 = false;}
if (tempY > 1000){continue1 = false;}
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}
if(continue1 != false){
//alert(tempX+', '+tempY);
window.location="./whichpixel.php?co="+tempX+","+tempY;
}
}
// End -->
</script>

It seems to be doing the job fine at the moment. It needs to limit the area to a 1000px by 1000px image, thus all the 1000 etc and the offset of the image on the page…

One of my big problems was getting the mouse position on the page, document as a whole, rather than the screen. Thus, if someone scrolled I was screwed. Adding document.documentElement.scrollLeft; and document.documentElement.scrollTop; to the x, y of the mouse coords respectively sorted this.

Due to the document.onclick = getMouseXY; it isn’t called, and is just loaded in the <head>

Let me know if its crap, as I built it rather hap-hazardly…