in Geeklife

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 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.

Write a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.


  • 879342 Blog Verification

    879342 Blog Verification…


  • Gubatron

    Another Reason why I hate IE – Getting Mouse Coordinates…

    Hi Angel Leon!!!,Our RSS engine has fetched and linked this post to our homepage (, if the post gets enough attention this week, we’ll talk about it on our next podcast (…