Mouse position in FireFox / NS

Discussion in 'Javascript' started by scot_nery, Aug 8, 2005.

  1. scot_nery

    scot_nery Guest

    I got this working in all browsers but FF/NS. It's not picking up the
    event to find the mouse position.

    In a perfect world, you mouse over the link and a mouse bubble pops up.

    Thanks for help.
    scot
     
    scot_nery, Aug 8, 2005
    #1
    1. Advertising

  2. scot_nery

    scot_nery Guest

    scot_nery, Aug 8, 2005
    #2
    1. Advertising

  3. "scot_nery" <> wrote in message
    news:...
    > Crap... http://dev.juggle.com/bubbletest.html
    > there.
    >


    If you copy this one, make sure you put the mouse bubble a bit away from the
    mouse location, other wise dragging the mouse from left to right over the
    link makes the bubble flash. (Try it). Probably because as you drag the
    mouse right it is no longer over the link, but over the mouse bubble
    instead, and thus onmouseout is called and the bubble dissapears.

    Ross
     
    Ross Marchant, Aug 8, 2005
    #3
  4. scot_nery <> wrote in message news:...
    > I got this working in all browsers but FF/NS. It's not picking up the
    > event to find the mouse position.
    >


    // It's best to monitor mouse co-ordinates with a dedicated handler:

    var mouseX, mouseY;

    function getMousePos(e)
    {
    if (!e)
    var e = window.event||window.Event;

    if('undefined'!=typeof e.pageX)
    {
    mouseX = e.pageX;
    mouseY = e.pageY;
    }
    else
    {
    mouseX = e.clientX + document.body.scrollLeft;
    mouseY = e.clientY + document.body.scrollTop;
    }

    }

    // You need to tell Mozilla to start listening:

    if(window.Event && document.captureEvents)
    document.captureEvents(Event.MOUSEMOVE);

    // Then assign the mouse handler

    document.onmousemove = getMousePos;

    // Then your mouseover function can just read mouseX and mouseY directly.

    --
    Stephen Chalmers http://makeashorterlink.com/?H3E82245A
     
    Stephen Chalmers, Aug 8, 2005
    #4
  5. scot_nery

    scot_nery Guest

    scot_nery, Aug 9, 2005
    #5
  6. Stephen Chalmers wrote :
    > scot_nery <> wrote in message news:...
    >
    >>I got this working in all browsers but FF/NS. It's not picking up the
    >>event to find the mouse position.
    >>

    >
    >
    > // It's best to monitor mouse co-ordinates with a dedicated handler:
    >
    > var mouseX, mouseY;
    >
    > function getMousePos(e)
    > {
    > if (!e)
    > var e = window.event||window.Event;
    >


    This manner of coding is not recommendable, IMO. In javascript strict
    mode (reporting warnings), Firefox will report that "variable e hides
    argument". This manner of coding just makes debugging more difficult.

    I recommend
    var TheEventObject;
    if(e)
    {
    TheEventObject = e;
    }
    else if(window.event)
    {
    TheEventObject = window.event;
    }
    else
    {
    TheEventObject = null;
    };

    > if('undefined'!=typeof e.pageX)
    > {
    > mouseX = e.pageX;
    > mouseY = e.pageY;
    > }
    > else
    > {
    > mouseX = e.clientX + document.body.scrollLeft;


    This will work in MSIE 5 but what if the document triggers standards
    compliant mode in MSIE 6? In such case, the provided code will not
    succeed as the root element is not the same.
    As written, the code [indirectly] is not promoting web standards. As
    written, the code is more backward-compatible than forward-compatible.

    > mouseY = e.clientY + document.body.scrollTop;
    > }
    >
    > }
    >
    > // You need to tell Mozilla to start listening:
    >
    > if(window.Event && document.captureEvents)
    > document.captureEvents(Event.MOUSEMOVE);
    >


    Why not register the listener to the object with DOM 2 Events method?
    It's forward-compatible. Future-proof.

    Gérard
    --
    remove blah to email me
     
    Gérard Talbot, Aug 12, 2005
    #6
  7. Gérard Talbot <> wrote in message news:...
    > Stephen Chalmers wrote :
    > > scot_nery <> wrote in message news:...
    > >
    > > var mouseX, mouseY;
    > >
    > > function getMousePos(e)
    > > {
    > > if (!e)
    > > var e = window.event||window.Event;
    > >

    >
    > This manner of coding is not recommendable, IMO. In javascript strict
    > mode (reporting warnings), Firefox will report that "variable e hides
    > argument". This manner of coding just makes debugging more difficult.


    That level of warning is intended to advise of possible pitfalls, not errors. I know that if I choose conditionally to
    overwrite a passed parameter, then I must be aware that any initial value may be lost.

    > > if('undefined'!=typeof e.pageX)
    > > {
    > > mouseX = e.pageX;
    > > mouseY = e.pageY;
    > > }
    > > else
    > > {
    > > mouseX = e.clientX + document.body.scrollLeft;

    >
    > This will work in MSIE 5 but what if the document triggers standards
    > compliant mode in MSIE 6?


    That can happen only if the programmer brings it about, in which case he will know what to expect and amend the code
    accordingly. I didn't want to clutter my demonstration with the cascade of questions that must be asked to cover that
    contingency.

    > >
    > > // You need to tell Mozilla to start listening:
    > >
    > > if(window.Event && document.captureEvents)
    > > document.captureEvents(Event.MOUSEMOVE);
    > >

    >
    > Why not register the listener to the object with DOM 2 Events method?
    > It's forward-compatible. Future-proof.
    >


    Here I must bow to your clairvoyancy, however I don't seem to know the syntax for making it past-proof, which remains a
    concern. On the planet where all users upgrade at the first opportunity, your advice may be of some value, but not where
    I reside.

    --
    Stephen Chalmers
    547265617375726520627572696564206174204F2E532E207265663A205451323437393134
     
    Stephen Chalmers, Aug 13, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    2
    Views:
    1,316
    Jim Corey
    Apr 23, 2004
  2. Max
    Replies:
    7
    Views:
    9,126
  3. tom arnall
    Replies:
    6
    Views:
    1,284
    Ian Shef
    Jan 18, 2007
  4. James Black
    Replies:
    0
    Views:
    403
    James Black
    May 28, 2006
  5. Replies:
    1
    Views:
    107
    Richard Cornford
    Jul 7, 2006
Loading...

Share This Page