Results 1 to 6 of 6

Thread: e.pageY in IE?

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default e.pageY in IE?

    How can you achieve the effect of e.pageY in IE? (not e.clientY)
    Trinithis

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    event.clientY
    However, it appears as though IE 7 at least adds 2px in both dimensions (nothing to worry about in most cases). And, because client refers to the window (not the page), any scroll state must also be taken into account. Here's a demo loosely adapted from DD's Image w/ description tooltip:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    body {
    margin:0;
    padding:0;
    }
    </style>
    <script type="text/javascript">
    function trackMouse(evt) {
    	var xbox=document.getElementById('xb'), ybox=document.getElementById('yb'),
    	mouseX, mouseY,
    	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body; //create reference to common "body" across doctypes
    	mouseX = evt&&evt.pageX? evt.pageX: window.event? window.event.clientX + standardbody.scrollLeft : 0;
    	mouseY = evt&&evt.pageY? evt.pageY: window.event? window.event.clientY + standardbody.scrollTop : 0;
    	xbox.value=mouseX;
    	ybox.value=mouseY;
    }
    onload=function(){
    document.onmousemove=trackMouse;
    }
    </script>
    </head>
    <body>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    X:<input type="text" id="xb"><br>Y:<input type="text" id="yb">
    <div style="cursor:default;position:absolute;top:10px;left:10px;border:1px solid black;">Hi</div>
    </body>
    </html>
    The upper left corner of the box with 'Hi' in it should be 10,10 (12,12 in IE).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Thanks. The following is what I'm going to use, and I hope it will be fine:

    Code:
    var standardBody = (document.compatMode=="CSS1Compat")? document.documentElement: document.body; //This won't contaminate anything; I'm using a closure (along with some other code, like the event handler)
    
    function abc(e) {
    	if(!e) e = window.event;
    	if(e.pageX===undefined) {
    		e.pageX = e.clientX + standardBody.scrollLeft;
    		e.pageY = e.clientY + standardBody.scrollTop;
    		}
    	//etc.
    	}
    Trinithis

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I would formally declare e:

    Code:
    var e=e||window.event;
    Other than that, looks fine, as long as you are sure that it will be used only with a mouse event, or at least one that generates coordinates.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    I would formally declare e:

    Code:
    var e=e||window.event;
    Does that have any particular advantages over !e?
    Trinithis

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No, it does have advantages over not declaring the variable. You could as easily use:

    Code:
    if (!e) var e = window.event;
    The important thing is to keep e out of the global scope, that's all.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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