Mouse event (onclick)

M

masantha wee

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="StdDevButton" type="button"
value="Standard Deviation Value" onclick="readStdDevValue()"/>

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="javascript">
document.addEventListener("mousemove", calculatePos, false);
function init() {
}
function getMousePosition(event) {
var posX = 0;
var posY = 0;
posX = event.clientX + document.body.scrollLeft;
posY = event.clientY + document.body.scrollTop;
var position = new Array(2);
position[0]  = posX;
position[1]  = posY;
return position;
}
function calculatePos (event) {
var mousePosition = getMousePosition(event);
checkAreaIn(mousePosition);
}
function checkAreaIn(mousePosition) {
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
 
M

mwh

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="StdDevButton" type="button"
value="Standard Deviation Value" onclick="readStdDevValue()"/>


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="someImageSource.jpg" width=241
useMap=#newsplash border=0>

<MAP name=newsplash>

<AREA
onClick="window.alert('Working!!!')"
shape=POLY target=_top
coords=1,2,1,46,78,48,80,197,240,201,239,18,93,12,94,2 <!--
example of coords -->
<AREA
onClick="window.alert('Working!!!')"
shape=RECT target=_top
coords=someCoords
<AREA
onClick="window.alert('Working!!!')"
shape=RECT target=_top
coords=someCoords
<AREA
onClick="window.alert('Working')"
shape=RECT target=_top
coords=someCoords</MAP>



</BODY>
</HTML>

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

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

Thomas 'PointedEars' Lahn

masantha said:
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="StdDevButton" type="button"
value="Standard Deviation Value" onclick="readStdDevValue()"/>

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>[/QUOTE]

The DOCTYPE declaration is missing.
[QUOTE]
<head>
<script language="javascript">[/QUOTE]

<script type="text/javascript">

[QUOTE="see"]
document.addEventListener("mousemove", calculatePos, false);[/QUOTE]

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

Most certainly to be removed.  See below.
[QUOTE]
function getMousePosition(event) {
var posX = 0;
var posY = 0;
posX = event.clientX + document.body.scrollLeft;
posY = event.clientY + document.body.scrollTop;[/QUOTE]

To make it work in IE 4+, use

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

prior.
[QUOTE]
var position = new Array(2);[/QUOTE]

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.)
[QUOTE]
position[0]  = posX;
position[1]  = posY;[/QUOTE]

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
[QUOTE]
return position;
}
function calculatePos (event) {
var mousePosition = getMousePosition(event);
checkAreaIn(mousePosition);
}
function checkAreaIn(mousePosition) {
var mx = mousePosition[0];
var my = mousePosition[1];
window.status = "X= " + mx + " Y= " + my;[/QUOTE]

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

else {
}[/QUOTE]

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

Since init() does nothing, remove it and the call (and
the attribute), or let it contain meaningful content.
[QUOTE]
</body>
</html>

Could anyone tell me how could I do it, please?[/QUOTE]

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


HTH

PointedEars
 
R

Richard Cornford

Thomas 'PointedEars' Lahn wrote:
You are trying to assign the event listener before
you defined it. This is not going to work.
<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top