473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

browser window coordinates

Is there a method in JavaScript that will detect and return the coordinates
of a mouse click? For example, if someone clicked the mouse within the
browser window, the method would return (453,125), or something similar. I
guess another way to ask this would be, is there a way to image map the
entire browser display as a whole?
--
Thomas J. Storm
Jul 23 '05 #1
2 3126
Here's what I have in one of my pages:

Put this function in an appropriate place:
function coordinates(){window.status="X=" + (event.clientX +
window.document.body.scrollLeft) + " Y=" + (event.clientY +
window.document.body.scrollTop);}

And then this:
<body onmousemove='coordinates()'>

On Tue, 17 Aug 2004 19:07:12 -0400, "Thomas Storm" <us**@network.net>
wrote:
Is there a method in JavaScript that will detect and return the coordinates
of a mouse click? For example, if someone clicked the mouse within the
browser window, the method would return (453,125), or something similar. I
guess another way to ask this would be, is there a way to image map the
entire browser display as a whole?


Jul 23 '05 #2
Martin wrote:
Here's what I have in one of my pages:

Put this function in an appropriate place:
That is an invitation for flippant comments, I will resist :)
function coordinates(){window.status="X=" + (event.clientX +
window.document.body.scrollLeft) + " Y=" + (event.clientY +
window.document.body.scrollTop);}

And then this:
<body onmousemove='coordinates()'>


And a version that works on more browsers than just IE in quirks mode
might go:-

/* MoveFollow monitors mousemove events and provides an interface
to the normalised page-relative co-ordinates form those events.
It initially applies its configuration logic to account for
browser differences and then assigns a simple function to the
onmousemove property of the document. Mouse move events tend to
happen extremely rapidly and need to be processed by a quick
and simple function, but most code that needs to know the
position of the mouse does not need that information nearly as
often so it is likely to be using setTimeout, or triggered by
other events.
*/
function MoveFollow(){
var global = this;
var theOne = {
/* Returns the page relative x co-ordinate of the mouse from
the last processed event (usually a mousemove event):-
*/
getPageX:function(){
return ((readScroll.scrollLeft|0) -
(readScroll.clientLeft|0) + x);
},
/* Returns the page relative y co-ordinate of the mouse from
the last processed event (usually a mousemove event):-
*/
getPageY:function(){
return ((readScroll.scrollTop|0) -
(readScroll.clientTop|0) + y);
},
/* Returns an object with public - x- and - y - properties as
would be returned by calls to - getPageX - and - getPageY
- respectivly:-
*/
getPageXY:function(){
return {x:this.getPageX(),y:this.getPageY()};
},
/* Returns a reference to the normalised - target/srcElement -
Node from the last processed event object:-
*/
getTarget:function(){
return lastTarget;
},
/* Allows additional events to be processed by the same position
reading method, thus updating the values returned from
- getPageX - and - getPageY -. It may be used with other mouse
co-ordinate using events such as onmousedown/up to avoid having
to repeate the decision making logic elsewhere in a scirpt:-
*/
upDate:function(ev){
mm(ev);
}
};
var x = 0, y = 0;
var readScroll = {scrollTop:0,scrollLeft:0,
clientLeft:0,clientTop:0};
var lastTarget = null;
var posReadX = 'pageX';
var posReadY = 'pageY';
function mm(e){
e = e||global.event;
x = e[posReadX];
y = e[posReadY];
lastTarget = e.target||e.srcElement;
}
function initEvent(ev){
ev = ev||global.event;
if(ev){
if(typeof ev.pageX != 'number'){ //Opera 7 has pageX
posReadX = 'clientX';
posReadY = 'clientY';
if(!global.opera){ //Not an Opera browser
if((typeof document.compatMode == 'string')&&
(document.compatMode.indexOf('CSS') != -1)&&
(document.documentElement)){
readScroll = document.documentElement;
}else if(document.body){
readScroll = document.body;
}
}
}
setUpOnMouse(mm);
mm(ev);
}else{
setUpOnMouse(initEvent);
}
}
function setUpOnMouse(f){
if(document.onmousemove != f){
document.onmousemove = f;
if((document.captureEvents)&&(global.Event)&&
(document.layers)&&(typeof Layer == 'function')){
document.captureEvents(Event.MOUSEMOVE);
}
}
}
initEvent();
return ((MoveFollow = function(){return theOne;})());
};

Usage, acquire a reference to the mouse position reporting interface.
There is only one interface object so all calls to MoveFollow return a
reference to that one interface object:-

var mouseMoveInterface = MoveFollow();

Code is expected to hold on to its reference to the interface object so
that it can be re-used on subsequent calls.

Co-ordinates are read by calling methods on the interface:-

var xPos = mouseMoveInterface.getPageX();

- the returned value will be the X co-ordinate of the last mousemove
event.

Similarly:-

var yPos = mouseMoveInterface.getPageY();

The method - upDate - allows other event object to use the same code to
acquire normalised page relative co-ordinates form an event object:-

ev = ev||window.event;
mouseMoveInterface.upDate(ev);
var yPos = mouseMoveInterface.getPageY(); // yPos from - ev - event
object.

Richard.


Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Suresh Kumar | last post by:
Hi, In Tkinter, how to convert canvas coordinates to window coordinates? To convert window coordinates to canvas coordinates we are using "canvasx,canvasy". But how to do vice-versa? Iam using a...
0
by: Suresh Kumar | last post by:
Hi, In Tkinter, how to convert canvas coordinates to window coordinates? To convert window coordinates to canvas coordinates we are using "canvasx,canvasy". But how to do vice-versa? With...
4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
20
by: Mandy Memphis | last post by:
If I perform a mousedown within a document, move the mouse outside the browser window, and then release the mouse button, the document.onmouseup event does not fire. Is there any way to detect a...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
1
by: igpig | last post by:
Sorry if this has been asked before. I've searched the group and the FAQ and didn't find the answer. How can I retrieve x, y coordinates of the browser top left corner in Internet Explorer? ...
1
by: Dave | last post by:
I'm running the below script to open a popup window. Works great! How Can I get "Mouse Coordinates" (X,Y) to include them as Left and Top parameters so that the popup window opens "where the...
0
by: a4andy | last post by:
Hi, This has got me pulling my hair out, all I want to do is get the cursor position within a client window in the same units as the window coordinates. If I use; Private Declare Function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.