Mouseposition in Netscape7

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dade Murphy

    Mouseposition in Netscape7

    Hi everyone!

    I've got a little problem and just cann't find any solution for it: I
    need to get the current position of the mousecursor at my webpage for
    some overlay stuff. Currently I use the following code for this:

    if (ieDom)
    {
    // Internet Explorer
    X=window.event. clientX + document.body.s crollLeft;
    Y=window.event. clientY + document.body.s crollTop;
    }
    else if (nsDom)
    {
    // Netscape 4.xx
    X=event.screenX + window.pageXOff set;
    Y=event.screenY + window.pageYOff set;
    }
    else if(W3CDom)
    {
    // Netscape 6, 7, Mozilla, Opera, etc.
    ?????????
    }

    So... can anybody tell me what code to use for the NS6 part? screenX
    is redefined in Gecko no longer being the position of the mousecursor
    but the position of the browser window.

    I would appreciate any help.
    Dade
  • Lasse Reichstein Nielsen

    #2
    Re: Mouseposition in Netscape7

    dade_murphy@gmx .at (Dade Murphy) writes:
    [color=blue]
    > Hi everyone!
    >
    > I've got a little problem and just cann't find any solution for it: I
    > need to get the current position of the mousecursor at my webpage for
    > some overlay stuff. Currently I use the following code for this:
    >
    > if (ieDom)[/color]

    Don't switch on what you suspect is the dom. Just og for the
    properties themselves. I usually use this:

    var root = document.docume ntElement||docu ment.body;
    var pageX = event.pageX || event.clientX + root.scrollLeft ;
    var pageY = event.pageY || event.clientY + root.scrollTop;

    Your code only uses document.body, which fails in IE in standards mode
    (and you *should* write new pages to standards mode!).

    Both Netscape 4 and Mozilla (and Opera 7) supports "event.page X",
    while IE supports the W3C DOM "clientX" property and the proprietary
    "scrollLeft ".


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dade Murphy

      #3
      Re: Mouseposition in Netscape7

      Hi again!

      Just tried the code you sent. Looks good with NS4 and IE, but still
      doesn't work with NS6, NS7 and Mozilla. The mouseposition still
      remains unset and the browser gets me some JS exceptions.

      Would appreciate further ideas.
      Dade

      Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<oeszsnka. fsf@hotpop.com> ...[color=blue]
      > dade_murphy@gmx .at (Dade Murphy) writes:
      >[color=green]
      > > Hi everyone!
      > >
      > > I've got a little problem and just cann't find any solution for it: I
      > > need to get the current position of the mousecursor at my webpage for
      > > some overlay stuff. Currently I use the following code for this:
      > >
      > > if (ieDom)[/color]
      >
      > Don't switch on what you suspect is the dom. Just og for the
      > properties themselves. I usually use this:
      >
      > var root = document.docume ntElement||docu ment.body;
      > var pageX = event.pageX || event.clientX + root.scrollLeft ;
      > var pageY = event.pageY || event.clientY + root.scrollTop;
      >
      > Your code only uses document.body, which fails in IE in standards mode
      > (and you *should* write new pages to standards mode!).
      >
      > Both Netscape 4 and Mozilla (and Opera 7) supports "event.page X",
      > while IE supports the W3C DOM "clientX" property and the proprietary
      > "scrollLeft ".
      >
      >
      > /L[/color]

      Comment

      • Richard Cornford

        #4
        Re: Mouseposition in Netscape7

        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:oeszsnka.f sf@hotpop.com.. .[color=blue]
        > dade_murphy@gmx .at (Dade Murphy) writes:[/color]
        <snip>[color=blue]
        > var root = document.docume ntElement||docu ment.body;
        > var pageX = event.pageX || event.clientX + root.scrollLeft ;
        > var pageY = event.pageY || event.clientY + root.scrollTop;
        >
        >Your code only uses document.body, which fails in IE in standards
        >mode (and you *should* write new pages to standards mode!).[/color]

        I have mentioned before that your root element determining method is
        wrong for IE 5.0 because it has enough of a DOM implementation to have a
        documentElement but the root element is _always_ document body.

        I don't think that code that needs to choose the root element has much
        choice but to either take the line used in the FAQ and see if
        documentElement has a non-zero client (or offset) Width/Height prior to
        using it, or follow the, IMO better, technique of examining the
        document.compat Mode property for its existence and content.
        [color=blue]
        >Both Netscape 4 and Mozilla (and Opera 7) supports "event.page X",
        >while IE supports the W3C DOM "clientX" property and the
        >proprietary "scrollLeft ".[/color]

        There is a (default) two pixel discrepancy between - event.pageX - and
        event.clientX+r oot.scrollLeft - due to IE including the inner border of
        the viewPort in its mouse co-ordinates. It can be corrected for by
        subtracting root.clientLeft (and clientTop for Y) from the mouse posting
        values (bringing the results into line with the co-ordinates use for
        pageX and positioning elements on the page). But because some browsers
        do not provide a clientLeft on the root element it would be safer to
        default it to zero if undefined. Assuming a browser window size of less
        than 2^31 pixels a binary NOT zero operation would be both quick and
        safe:-

        event.clientX + root.scrollLeft - (root.clientTop |0)

        There is also a bug in Opera <= 6 where the clientX/Y values of the
        event objects are equivalent to pageX/Y values on other browsers. But
        Opera <= 6 does not provide pageX/Y so it has to be arranged that the
        scrollLeft/Top and clientLeft/Top are not applied to Opera <=6. This is
        the one occasion where I have seen a reason for using the window.opera
        property as Opera 7 has PageX/Y so it takes the first option anyway.

        Richard.


        Comment

        Working...