Skip to content

Instantly share code, notes, and snippets.

@shamasis
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamasis/beec7a77c7731023ac17 to your computer and use it in GitHub Desktop.
Save shamasis/beec7a77c7731023ac17 to your computer and use it in GitHub Desktop.
Cross-browser pageX and pageY of events
/**
* This function ensures that the event object always has pageX and pageY irrespective of the
* browser. Older IE browsers do not support pageXY.
*
* @param {MouseEvent} event
* @returns {MouseEvent} This function updates the events `pageX` and `pageY` properties when
* they're missing and also returns the same event for the sexy programming styles.
*/
var getEventCoordinate = (function () {
var PAGEX = 'pageX',
body;
return function (event) {
// If `pageX` is undefined, it is certain that we need to fallback to `clientX`.
// There is no point separately checking for `pageY`.
if (PAGEX in event) {
// We store reference to body while accessing so that future references are fast.
event.pageX = event.clientX + (body || (body = window.document.body ||
window.document.documentElement)).scrollLeft;
event.pageY = event.clientY + body.scrollTop;
}
return event;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment