Problem with Opera an IE

S

Si

Hi,

I'll start by saying I'm a complete newbie, this is the first time I
have used Javascript.

The code below I have written to calculate distance/bearings and times
from a grid reference.

It works absolutely fine in Opera 7.11, but in IE it throws up an
"object doesn't support this operation or method" error.

Any help/suggestions to where I have gone wrong and how I can get this
working in IE would be greatly appreciated.

Cheers Si,

###############CODE###################

function distance(form) {
var x1 = eval(form.x1.value);
var y1 = eval(form.y1.value);
var x2 = eval(form.x2.value);
var y2 = eval(form.y2.value);
var sd = eval(form.s.value);
var xdiff = x2 - x1;
var ydiff = y2 - y1;
var y = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5)/10;
if (Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) - 90 < 0) {
var b = Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) + 270;
}
else {
var b = Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) - 90;
}
var t = y / sd;
form.distance.value = Math.round(10*y)/10
form.time.value = (Math.round(10*t)/10)*60
form.bear.value = Math.round(b)
}
###########################################
 
L

Lasse Reichstein Nielsen

The code below I have written to calculate distance/bearings and times
from a grid reference.

It works absolutely fine in Opera 7.11, but in IE it throws up an
"object doesn't support this operation or method" error.

It would be very helpful to know which line the error comes from,
what the form contains and how the function is called.

function distance(form) {
var x1 = eval(form.x1.value);

The "eval" function is a very slow way to turn a string into a number
(by a factor of 80 in some browsers). The fastest way us to use a
unary prefix plus. I.e.,

var x1 = +(form.x1.value);

You will probably never need to use "eval" for anything. There are
better and faster ways of solving most problems that a normal page
author runs into. Don't trust solutions that use "eval".
form.distance.value = Math.round(10*y)/10

You have an input element with the name "distance". IE makes a global
variable with that name that refers to the input element. However,
that overwrites your function called "distance". Change the name
of either the input element or the function.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
if (Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) - 90 < 0) {
var b = Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) + 270;
}
else {
var b = Math.atan2((y2 - y1),-(x2 - x1)) * (180 / Math.PI) - 90;
}


If that's to convert (X, Y) to bearing from North, there must be an
easier way. Try exchanging X & Y, to get +-180 deg from North.

B = Math.atan2(X, Y)*180/Math.PI // -180<B<=+180
B = (360+Math.atan2(X, Y)*180/Math.PI)%360 // 0<=B<360

Unless the span is certainly small, you should warn about approximations
die to the Non-Flat Earth.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top