Events in Mozilla and firefox?

  • Thread starter Michael Christensen
  • Start date
M

Michael Christensen

The following works fine in IE -> what am I missing to make it work in
Firefox - an example would be great?

I can't figure it out :)
------------------------------------------------
<body>

<script language="javascript">
var IE = document.all?true:false;
if (!IE) document.captureEvents(event.MOUSEMOVE);
document.onmousemove = getMouseXY;

var x,y,tmpcords;

function getMouseXY(e) {
if(IE){
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
} else {
x = e.pageX;
y = e.pageY;
}

//Check if length is above 1200
if(document.all._clientEyeTextbox.value.length > 1200){
return calc();
document.all._clientEyeTextbox.value = '';
} else {
document.all._clientEyeTextbox.value =
document.all._clientEyeTextbox.value + x + ',' + y + ',';
//document.all.a.value = document.all.a.value + x + ',' + y + ',';
}
}
</script>

</body>
 
R

Randy Webb

Michael Christensen said the following on 10/24/2005 2:08 PM:
The following works fine in IE -> what am I missing to make it work in
Firefox - an example would be great?

I can't figure it out :)

Try Tools>Javascript Console
Stop assuming that the existence of document.all means you are dealing
with IE - it doesn't.
Stop thinking you need to know what browser you are dealing with - you
don't.
Start feature detecting.
 
R

RobG

Michael said:
The following works fine in IE -> what am I missing to make it work in
Firefox - an example would be great?

I can't figure it out :)

The language attribute is depreciated, type is required:

var IE = document.all?true:false;

When posting code, indent using 2 or 4 spaces, not tabs. It helps to
stop wrapping which nearly always introduces errors. Manually wrap code
at about 70 characters (same reason).

As Randy said, browser detection based on whether or not certain
functions are supported is flawed. Simply don't do it.

if (!IE) document.captureEvents(event.MOUSEMOVE);

For some reading about how/when to use captureEvents:

<URL:http://www.quirksmode.org/js/events_order.html>
<URL:http://www.quirksmode.org/js/events_netscape4.html>

Remove the -- if (!IE)... -- line...

document.onmousemove = getMouseXY;

And add this one here...

if (document.captureEvents){
document.captureEvents(Event.MOUSEMOVE);
}
var x,y,tmpcords;

It's probable that these globals aren't needed. If the issue is
returning multiple values from a function, you can do that by putting
them into an array and return that.

function getMouseXY(e) {
if(IE){
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
} else {
x = e.pageX;
y = e.pageY;
}

Once you remove browser sniffing, the above becomes:

function getMouseXY(e)
{
var e = e || window.event;
if (e.pageX || e.pageY){
x = e.pageX;
y = e.pageY;
} else if (e.clientX || e.clientY) {
x = event.clientX + document.body.scrollLeft;
y = event.clientY + document.body.scrollTop;
}

//Check if length is above 1200
if(document.all._clientEyeTextbox.value.length > 1200){

What is _clientEyeTextbox? If it's the name of a form control, then the
best way to access it is to get a reference then use that ('formName' is
the name of the form):

var textBox = document.forms.formName._clientEyeTextbox;
if (1200 < textBox.value){


If _clientEyeTextBox is the id of the control, then it's invalid since
id attributes must start with a letter (it's OK for a name to start with
an underscore, but certain valid name characters will cause problems in
places depending on how you access the control). Read the HTML spec on
name and id attributes.

return calc();

The function will end here if the if statement returned true.
document.all._clientEyeTextbox.value = '';

This line will not be reached, but if it is was it should be:

textBox.value = '';


} else {
document.all._clientEyeTextbox.value =
document.all._clientEyeTextbox.value + x + ',' + y + ',';

textBox.value += x + ',' + y + ',';

Since you seem to be storing x and y in the text box, they need not be
global variables. And if the text box is a device for passing the
values to some other function, you could use:

return [x, y];

to return the values in an array - but my assumption may be a wrong.

//document.all.a.value = document.all.a.value + x + ',' + y + ',';
}
}
</script>

</body>

All the above is untested - if you provide some working code with
associated HTML (minimal working example) then better help can be provided.
 
V

VK

Events in Mozilla and firefox?

A bit offtopic but important to mention:
The question sounds similar to: "Difference between mammals and cows".
Any modern graphical browser is Mozilla meaning it has "Mozilla" in its
USER_AGENT string.
 

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,780
Messages
2,569,608
Members
45,246
Latest member
softprodigy

Latest Threads

Top