Mozilla & Mouse coordinates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Mozilla & Mouse coordinates

    How can I get the mouse coordinates in Mozilla?

    I try
    x = event.pageX
    but it does not work...
    basically I try
    alert(event) and it does not work.
    Where is the problem?
    the full code
    ------------------------
    <script>
    var obj = document.getEle mentById('toolt ip');
    if (navigator.appN ame == "Microsoft Internet Explorer") isIE = true; else
    isIE = false;

    function handlerMM(e){
    if (isIE) { x = event.pageX; y = event.pageY; }
    else {
    alert(event);
    x = event.clientX + document.body.s crollLeft;
    y = event.clientY + document.body.s crollTop;
    }
    obj.style.top = y + 10;
    obj.style.left = x + 15;
    }
    </script>


  • Keith Bowes

    #2
    Re: Mozilla &amp; Mouse coordinates

    <- Chameleon -> wrote:[color=blue]
    >
    > function handlerMM(e){
    > if (isIE) { x = event.pageX; y = event.pageY; }
    > else {
    > alert(event);[/color]

    "event" is undefined. An instance of the Event interface is passed as
    the argument e.
    [color=blue]
    > x = event.clientX + document.body.s crollLeft;[/color]
    x = e.screenX;
    [color=blue]
    > y = event.clientY + document.body.s crollTop;[/color]

    y = e.screenY;
    [color=blue]
    > }
    > obj.style.top = y + 10;
    > obj.style.left = x + 15;
    > }[/color]

    A unit must be concatenated to the length. For example, y + 10 + 'px'

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Mozilla &amp; Mouse coordinates

      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> writes:
      [color=blue]
      > How can I get the mouse coordinates in Mozilla?[/color]

      The eight way! (as opposed to how you do it in IE :)
      [color=blue]
      > I try
      > x = event.pageX
      > but it does not work...[/color]

      Sure it does, if the variable "event" refers to the event.
      In IE, "event" is a global variable that always refer to the
      current event, but that is an IE invention, and not all other
      browsers have emulated it.
      [color=blue]
      > <script>[/color]

      The type attribute is required in HTML 4.
      <script type="text/javascript">
      [color=blue]
      > var obj = document.getEle mentById('toolt ip');
      > if (navigator.appN ame == "Microsoft Internet Explorer") isIE = true; else
      > isIE = false;[/color]

      Browser detection is not a very good way to deduce Javascript
      functionality. Partly because browsers change between versions, and
      partly because some browsers lie about their name.
      [color=blue]
      > function handlerMM(e){[/color]

      The argument, "e", given to the function when it is called, is the
      event.
      I usually start my handlers like this:

      function handlerMM(event ) {
      event = event || window.event; // IE sucks!

      (yes, I usually write the comment too!).
      After this, you can use "event" to refer to the event in all browsers.
      [color=blue]
      > if (isIE) { x = event.pageX; y = event.pageY; }[/color]

      I guess you mean 'if (!isIE)', becuase pageX and pageY are not
      available in IE, but are in Mozilla. It would be more prudent to check
      for "pageX" directly:

      if (typeof event.pageX == "number") {
      x = event.pageX;
      y = event.pageY; // we dare expect that pageY exists if pageX does :)
      }[color=blue]
      > else {
      > alert(event);
      > x = event.clientX + document.body.s crollLeft;
      > y = event.clientY + document.body.s crollTop;[/color]

      If IE is in standards mode (and it *should* be!), then the html element
      is the root of the document tree, not body. In that case, you need to
      use document.docume ntElement instead of document.body.

      It's easier jo just use whatever is there, instead of trying to figure
      out whethere the page is begins shown according to standards.

      var root = document.docume ntElement || document.body;
      x = event.clientX + root.scrollLeft ;
      y = event.clientY + root.scrollTop;
      [color=blue]
      > }[/color]
      [color=blue]
      > obj.style.top = y + 10;[/color]

      This fails in browsers that follow the standard. CSS lengths must have
      a unit, and you are only assigning a pure number.
      obj.style.top = (y + 10) + "px";
      [color=blue]
      > obj.style.left = x + 15;[/color]

      ditto.

      /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

      • Lee

        #4
        Re: Mozilla &amp; Mouse coordinates

        <- Chameleon -> said:[color=blue]
        >
        >How can I get the mouse coordinates in Mozilla?
        >
        >I try
        >x = event.pageX
        >but it does not work...
        >basically I try
        >alert(event) and it does not work.
        >Where is the problem?
        >the full code
        >------------------------
        ><script>
        >var obj = document.getEle mentById('toolt ip');
        >if (navigator.appN ame == "Microsoft Internet Explorer") isIE = true; else
        >isIE = false;
        >
        >function handlerMM(e){
        > if (isIE) { x = event.pageX; y = event.pageY; }
        > else {
        >alert(event) ;
        > x = event.clientX + document.body.s crollLeft;
        > y = event.clientY + document.body.s crollTop;
        > }
        > obj.style.top = y + 10;
        > obj.style.left = x + 15;
        >}
        ></script>[/color]

        In Mozilla and other browsers, the Event object isn't a global
        variable. It's an argument that's passed to the event handler.
        In your example, it's probably "e".

        Comment

        Working...