Another Reason why I hate IE – Getting Mouse Coordinates

Fucking IE!!!

If I had a dollar for everytime I say that.

In anycase, I just fixed a bug which has been keeping all the people that visit wedoit4you.com from recommending articles by mail to their friends.

It has to do with getting the current position of the mouse.

Every other browser implementation considers the mouse position relative to the beginning of the the document, stupid microsoft developers were too lazy to add up the amount of scroll, and they not only were too lazy to do it like everyone else, their document.body.scrollTop function always returns 0, it turns out that you have to do document.documentElement.scrollTop to get the stupid scrollTop variable.

Yet another reason to hate IE’s guts even more.

So, kids, if you need a function that gives you the correct mouse position, relative to the beginning of your document, no matter how much you scrolldown, here it is:


var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.documentElement.scrollLeft
tempY = event.clientY + document.documentElement.scrollTop
//FUCKING IE, not only it considers mouse position
//relative to the current view, but it didn't retrieve
//the scroll value with document.body.scrollTop... MOTHERFUCKERS
} else { // grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}
// catch possible negative values in NS4
if (tempX < 0){tempX = 0} if (tempY < 0){tempY = 0} // show the position values in the form named Show // in the text fields named MouseX and MouseY return true }

Put that on one of the javascripts that you load, probably the first one actually, since the idea is to use global variables "tempX" and "tempY" to get the current mouse position at all times.

I learned about all this thanks to this post from Stephen Chapman.

2 thoughts on “Another Reason why I hate IE – Getting Mouse Coordinates

  1. Pingback: Gubatron

Leave a Reply

Your email address will not be published. Required fields are marked *