Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Capture true position of mouse

Posted on 2007-03-29
Medium Priority
Last Modified: 2011-10-03
How do you capture the TRUE position of the mouse in an html document with javascript? I tried using event.y for the Y coordinate, but the value doesn't increase when you scroll down the page. Thanks.
Question by:rbichon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 29

Expert Comment

by:Pravin Asar
ID: 18817869
you need to add document.scrollOffsetX document.scrollOffsetY to  mouse co-ordinates to
get it's position from top-left of the page.

Author Comment

ID: 18818366
document.scrollOffsetY comes up undefined.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 18818650
How do I get the height of the viewable window?

Expert Comment

ID: 18818775
LVL 29

Accepted Solution

Pravin Asar earned 1000 total points
ID: 18822860
<title>Window and Screen Size Details</title>
<body onscroll="reLocateDiv();">
<script language="javascript">
var wLeft = 0;
var wTop  = 0;
var wWidth=800;
var wHeight=600;
var scrWidth=0;
var scrHeight=0;

var wndDetail = new GetWindowDetails();
alert ('Screen Resolution : ' + wndDet.sWidth + ' X ' + wndDet.sHeight);
alert ('Window Location   : ' + wndDet.wLeft + ' , ' + wndDet.wTop);
alert ('Window Size       : ' + wndDet.wWidth + ' X ' + wndDet.wHeight);

function GetWindowDetails () {
this.wWidth = wWidth;
this.wHeight = wHeight;
this.sWidth = scrWidth;
this.sHeight = scrHeight;
this.wLeft = wLeft;
this.wTop  = wTop;
return this;
// Get Window Size
function WindowSize()
     if( typeof( window.innerWidth ) == 'number' ) {
          wWidth = window.innerWidth;
          wHeight = window.innerHeight;
     else if( document.documentElement &&
              (document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ) {
          //IE 6+ in 'standards compliant mode'
          wWidth = document.documentElement.clientWidth;
          wHeight = document.documentElement.clientHeight;
     else if( document.body && 
              (document.body.clientWidth ||
                 document.body.clientHeight ) ) {
          //IE 4 compatible
          wWidth = document.body.clientWidth;
          wHeight = document.body.clientHeight;

// Get Window Height
function GetWindowHeight () {
     return (wHeight-20);

// Get Window Width
function GetWindowWidth () {
     return (wWidth);

// Get Screen Resolution

function GetScreenResolution () {
   scrWidth = window.screen.width;
   scrHeight = window.screen.height;

// Get Window Location
function GetWindowLocation () {
    if (!document.layers && document.all) {
          wLeft = window.screenLeft;
          wTop = window.screenTop;
    else {
          wLeft = window.screenX;
          wTop  = window.screenY;

function resizeDiv()
   var wndDetail = new GetWindowDetails();
   var newWidth=wndDetail.wWidth - 100;
   if (newWidth>1000) newWidth=1000;

function rePositionDiv()
   var wndDetail = new GetWindowDetails();
   var divObj = document.getElementById("divMainContent");
   var newX= (parseInt (wndDetail.wWidth) - parseInt(divObj.style.width))/2;
   divObj.style.left = parseInt(newX);
   var newY= (parseInt (wndDetail.wHeight) - parseInt(divObj.style.height))/2;
   divObj.style.top = parseInt(newY);


function reLocateDiv () {
   var wndDetail = new GetWindowDetails();
   var divObj = document.getElementById("divMainContent");
   var newY= parseInt(orgPosY) +  parseInt (window.document.body.scrollTop);
   var newX= parseInt(orgPosX) +  parseInt (window.document.body.scrollLeft);
   window.status = newY + ' , ' + window.document.body.scrollTop;
   divObj.style.top =  parseInt(newY);
   divObj.style.left = parseInt(newX);
var divObj  = document.getElementById("divMainContent");
var orgPosY = 10;
var orgPosX = 10;
<span id="divMainContent" style="border: 1px solid black; position: absolute; top: 10px; left: 10px; width: 50px; height: 100px;">DIV OBJECT</span>
<div style="height: 1000px; width: 1000px; ">DIV</div>


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question