Mouse event (onclick)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • masantha wee

    #1

    Mouse event (onclick)



    Hi all,

    I am using Firefox and embedding Javascript in html. I understand that
    we can use mouse events by coding them in the body of html (by creating
    a button or anything and by adding in the events in the <img> tag).

    [code]
    <input id="StdDev Value" name="StdDevBut ton" type="button"
    value="Standard Deviation Value" onclick="readSt dDevValue()"/>

    But what if I need to add some mouse events without creating any img or
    button? What I mean is - when the mouse cursor is in certain coordinates
    (eg. in certain areas on an image), then if the user left click,
    something will happen. The beloow example code seems to be not working
    for me....

    <html>
    <head>
    <script language="javas cript">
    document.addEve ntListener("mou semove", calculatePos, false);
    function init() {
    }
    function getMousePositio n(event) {
    var posX = 0;
    var posY = 0;
    posX = event.clientX + document.body.s crollLeft;
    posY = event.clientY + document.body.s crollTop;
    var position = new Array(2);
    position[0] = posX;
    position[1] = posY;
    return position;
    }
    function calculatePos (event) {
    var mousePosition = getMousePositio n(event);
    checkAreaIn(mou sePosition);
    }
    function checkAreaIn(mou sePosition) {
    var mx = mousePosition[0];
    var my = mousePosition[1];
    window.status = "X= " + mx + " Y= " + my;
    if (mx > 20 && mx < 80 && my > 40 && my < 100) {
    this.onclick = "pop()";
    } else {
    }
    }
    function pop() {
    alert("working! !!");
    }
    </script>
    </head>
    <body onload="init(); ">
    </body>
    </html>

    Could anyone tell me how could I do it, please?
    Thanks in advance.

    seanky

    *** Sent via Developersdex http://www.developersd ex.com ***
  • mwh

    #2
    Re: Mouse event (onclick)

    masantha wee wrote-
    Hi all,


    I am using Firefox and embedding Javascript in html. I understand that
    we can use mouse events by coding them in the body of html (by creating

    a button or anything and by adding in the events in the <img> tag).


    [code]
    <input id="StdDev Value" name="StdDevBut ton" type="button"
    value="Standard Deviation Value" onclick="readSt dDevValue()"/>


    But what if I need to add some mouse events without creating any img or

    button? What I mean is - when the mouse cursor is in certain
    coordinates
    (eg. in certain areas on an image), then if the user left click,
    something will happen. The beloow example code seems to be not working
    for me....
    ....
    Could anyone tell me how could I do it, please?
    Thanks in advance.


    seanky


    well, there is a way to acomplish this using the <map> tag.

    <HTML>
    ....
    <IMG height=208 src="someImageS ource.jpg" width=241
    useMap=#newspla sh border=0>

    <MAP name=newsplash>

    <AREA
    onClick="window .alert('Working !!!')"
    shape=POLY target=_top
    coords=1,2,1,46 ,78,48,80,197,2 40,201,239,18,9 3,12,94,2 <!--
    example of coords -->[color=blue]
    >[/color]

    <AREA
    onClick="window .alert('Working !!!')"
    shape=RECT target=_top
    coords=someCoor ds[color=blue]
    >[/color]

    <AREA
    onClick="window .alert('Working !!!')"
    shape=RECT target=_top
    coords=someCoor ds[color=blue]
    >[/color]

    <AREA
    onClick="window .alert('Working ')"
    shape=RECT target=_top
    coords=someCoor ds[color=blue]
    >[/color]
    </MAP>



    </BODY>
    </HTML>

    Maybe this will help, I hope it does. This should work, but I a no
    javascript expert.

    (____)
    (\/)
    /-------\/
    / | MWH ||
    - ||----||

    Comment

    • Tony

      #3
      Re: Mouse event (onclick)

      "masantha wee" <samcctan@yahoo .co.uk> wrote in message
      news:X_Kce.4$6E 3.2529@news.usw est.net...[color=blue]
      >
      >
      > Hi all,
      >
      > I am using Firefox and embedding Javascript in html. I understand that
      > we can use mouse events by coding them in the body of html (by creating
      > a button or anything and by adding in the events in the <img> tag).
      >
      > [code]
      > <input id="StdDev Value" name="StdDevBut ton" type="button"
      > value="Standard Deviation Value" onclick="readSt dDevValue()"/>
      >
      > But what if I need to add some mouse events without creating any img or
      > button? What I mean is - when the mouse cursor is in certain coordinates
      > (eg. in certain areas on an image), then if the user left click,
      > something will happen. The beloow example code seems to be not working
      > for me....[/color]

      Um - can you clarify something?

      You say: "without creating any img", then say "eg. in certain areas on an
      image"

      Do you want an image or not?

      If so, then use the MAP and AREA tags to define an image map and assign that
      to the image.

      If not, then please clarify exactly what you're trying to do.


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Mouse event (onclick)

        masantha wee wrote:
        [color=blue]
        > I am using Firefox and embedding Javascript in html. I understand that
        > we can use mouse events by coding them in the body of html (by creating
        > a button or anything and by adding in the events in the <img> tag).
        >
        > [code]
        > <input id="StdDev Value" name="StdDevBut ton" type="button"
        > value="Standard Deviation Value" onclick="readSt dDevValue()"/>
        >
        > But what if I need to add some mouse events without creating any img or
        > button? What I mean is - when the mouse cursor is in certain coordinates
        > (eg. in certain areas on an image), then if the user left click,
        > something will happen. The beloow example code seems to be not working
        > for me....
        >
        > <html>[/color]

        The DOCTYPE declaration is missing.
        [color=blue]
        > <head>
        > <script language="javas cript">[/color]

        <script type="text/javascript">

        see <http://validator.w3.or g/> and numerous discussions here.
        [color=blue]
        > document.addEve ntListener("mou semove", calculatePos, false);[/color]

        You are trying to assign the event listener before you defined it.
        This is not going to work.
        [color=blue]
        > function init() {
        > }[/color]

        Most certainly to be removed. See below.
        [color=blue]
        > function getMousePositio n(event) {
        > var posX = 0;
        > var posY = 0;
        > posX = event.clientX + document.body.s crollLeft;
        > posY = event.clientY + document.body.s crollTop;[/color]

        To make it work in IE 4+, use

        if (!event)
        {
        event = window.event;
        }

        prior.
        [color=blue]
        > var position = new Array(2);[/color]

        JS arrays (Array objects) are dynamic by default and due to differences
        in implementations it is unwise to try to initialize them with a certain
        length; the argument could be interpreted as the value of the first and
        only numeric element.

        ... = new Array();

        or even

        ... = [];

        will suffice (although the latter defined in ECMAScript 3+ only and so
        not downwards compatible.)
        [color=blue]
        > position[0] = posX;
        > position[1] = posY;[/color]

        I'd rather define an Object object here:

        var position = {x: posX, y: posY};

        or

        var position = new Object();

        Access properties with

        position.x
        position.y
        [color=blue]
        > return position;
        > }
        > function calculatePos (event) {
        > var mousePosition = getMousePositio n(event);
        > checkAreaIn(mou sePosition);
        > }
        > function checkAreaIn(mou sePosition) {
        > var mx = mousePosition[0];
        > var my = mousePosition[1];
        > window.status = "X= " + mx + " Y= " + my;[/color]

        Leave my status bar alone, provided I even have one.
        [color=blue]
        > if (mx > 20 && mx < 80 && my > 40 && my < 100) {
        > this.onclick = "pop()";
        > }[/color]

        OK.
        [color=blue]
        > else {
        > }[/color]

        To be removed.
        [color=blue]
        > }
        > function pop() {
        > alert("working! !!");
        > }
        > </script>
        > </head>
        > <body onload="init(); ">[/color]

        Since init() does nothing, remove it and the call (and
        the attribute), or let it contain meaningful content.
        [color=blue]
        > </body>
        > </html>
        >
        > Could anyone tell me how could I do it, please?[/color]

        If images are not involved, the current approach appears to be viable
        (otherwise use image maps); however, the current realization is not.


        HTH

        PointedEars

        Comment

        • Richard Cornford

          #5
          Re: Mouse event (onclick)

          Thomas 'PointedEars' Lahn wrote:
          <snip>[color=blue][color=green]
          >> document.addEve ntListener("mou semove", calculatePos, false);[/color]
          >
          > You are trying to assign the event listener before
          > you defined it. This is not going to work.[/color]
          <snip>

          Subject to the W3C DOM events method being defined on the document in
          question, that will work. The - calculatePos - function takes the form
          of a function declaration within the same script element so the
          corresponding function object will be created and assigned to a
          'calculatePos' property of the global object during variable
          instantiation for the global execution context. That will happen, and be
          finished, before any global code is executed. Thus the above method call
          will receive a reference to that function object when the Identifier -
          calcuatePos - is resolved.

          Richard.


          Comment

          Working...