View Full Version : How to get the ClientX of ClientY attributes in Mozilla firefox

07-07-2006, 10:29 AM

I am not able to get the clientX (or pageX), clientY(or PageY) values in the OnBeforeUnload event . Since i am working on to find a solution for prompting the user on the exit of the window to save the unsaved information. Here i came to solution with getting the clientX and clientY values in onBeforeUnload event . It is working fine in IE but in MozillaFirefox , clientX and clientY are not senced in onBeforeUnload event.

So can any one suggest or throw some light on it. :)

07-07-2006, 10:49 AM
Sounds like you're only using IE's Event Model when you need to use both it and the W3C DOM2 Event Model

Understanding IE's Event Model (http://msdn.microsoft.com/workshop/author/om/event_model.asp)

07-07-2006, 11:12 AM
Thanks for you reply

Actually this is the code i am using right now it works for IE. But for the onBeforeUnload event in Mozilla Firefox it is not senceing clientX, clientY i have tried with PageX and PageY also .

<script language='javascript'>
window.onbeforeunload = confirmExit;
function confirmExit(event)
var windowWidth=document.body.clientWidth;
var x=window.event.clientX;
var y=window.event.clientY;
if (x>windowWidth/2 && y<0)
return "Are you sure you want to exit this page ?";
<form >
This is window exit and refresh

could you please try , if you could find a better solution for this

07-07-2006, 01:06 PM
Mozilla passes the event object as an argument to the listener, instead of having a global event object.

window.onload = function(event) {

For example. Though, I would be surprised if clientX is attached to a non-mouse event.

07-10-2006, 06:57 AM

Thank you for the information. But my scenario is to promt a message when we are exiting the window. I actually got a solution for it, but the problem was that it is handled with onBeforeUnLoad event so the prompt is getting displayed even for refresh of th page or click of any hyper link. That is the reason why i want the clientX clientY (Mouse positions) in onBeforeUnLoad event only. So that based on these values i could decide which button was clicked.

The other cases for the exit and refresh of window like the "alt + F4" and the "F5" keystrokes are already handled by me .

Even in the onBeforUnLoad event i found the Mouse Position values by handling the MouseMove event and storing the position values in the global variables . Here the problem was that when the mouse is moved faster then worng values are fetched because of time elapse.

If i could find the ClienX and ClientY values in onBeforeUnLoad event then i could find the solution for my requirement.

09-28-2006, 02:02 PM
It turns out that Firefox zeros out the clientX and clientY values, even in global variables when used in onbeforeunload. The work around is to set the values in global variables uing onmouse move, then reference work:

posX = null;
posY = null;

whereAmI = function() {
alert(posX + '\n' + poxY);

document.onmousemove = function(evt) {
if (typeof evt == 'undefined') {
myEvent = window.event;
} else {
myEvent = evt;
posX = myEvent.clientX;
posY = myEvent.clientY;

window.onbeforeunload = function() {

09-28-2006, 02:19 PM
You should detect the clientX/pageX attribute. And I guess you should compensate the possible scroll of the page

document.onmousemove = function(evt) {
if (typeof evt == 'undefined') {
myEvent = window.event;
} else {
myEvent = evt;
posX = myEvent.clientX+document.body.scrollLeft;
posY = myEvent.clientY+document.body.scrollTop;
else if(myEvent.pageX){
posX = myEvent.pageX+window.pageXOffset;
posY = myEvent.pageY+window.pageYOffset;

09-28-2006, 03:38 PM
The puzzle here is not that Mozilla returns [0,0] for the clientX & Y,
but that IE doesn't. beforeunload is not a mouse event, so there is no relevant cursor position.

IE reports null when you ask it to report the event.srcElement,
while the other browsers return HTMLDocumentObject for the event.target.

The document is always at 0,0- it is the context that all the other
x and y's are calculated from.

You should be checking to see how far the body has been scrolled, rather than the clientX and Y of the event source/target.

That brings me back to IE- it's like Rain Man, it has these odd talents
with no explanation. My guess is that it is parrotting the last global Event's clientX and Y
before the onbeforeunload event, but I can't nail it down.

09-28-2006, 11:09 PM
should work if the code is correct. Here's an example of capture the mouse position:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var posx;var posy;
function getMouse(e){
var ev=(!e)?window.event:e;//IE:Moz
if (ev.pageX){//Moz
else if(ev.clientX){//IE
else{return false}//old browsers
document.getElementById('myspan').firstChild.data='X='+posx+' Y='+posy;
<span id="myspan">&nbsp;</span>