Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to get the ClientX of ClientY attributes in Mozilla firefox

    Hi,

    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.

  • #2

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?";
    }
    </script>
    <HTML>
    <body>
    <form >
    This is window exit and refresh
    </form>
    </body>
    </HTML>

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

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Mozilla passes the event object as an argument to the listener, instead of having a global event object.

    window.onload = function(event) {
    alert(event.clientX);
    }

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

  • #5
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi JDK,

    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.

  • #6
    Banned
    Join Date
    Dec 2005
    Location
    That's a secret.
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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() {
    whereAmI();
    }

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You should detect the clientX/pageX attribute. And I guess you should compensate the possible scroll of the page

    Code:
    document.onmousemove = function(evt) {
    if (typeof evt == 'undefined') {
    myEvent = window.event;
    } else {
    myEvent = evt;
    }
    if(myEvent.clientX){
    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;
    }
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.
    Last edited by mrhoo; 09-29-2006 at 02:36 PM.

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    should work if the code is correct. Here's an example of capture the mouse position:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <head>
    <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){
    posx=0;posy=0;
    var ev=(!e)?window.event:e;//IE:Moz
    if (ev.pageX){//Moz
    posx=ev.pageX+window.pageXOffset;
    posy=ev.pageY+window.pageYOffset;
    }
    else if(ev.clientX){//IE
    posx=ev.clientX+document.body.scrollLeft;
    posy=ev.clientY+document.body.scrollTop;
    }
    else{return false}//old browsers
    document.getElementById('myspan').firstChild.data='X='+posx+' Y='+posy;
    }
    document.onmousemove=getMouse
    </script>
    </head>
    <body>
    <span id="myspan">&nbsp;</span>
    </body>
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •