PDA

View Full Version : JavaScript error code with NS only, HELP!!!


PhotoJoe
07-01-2002, 09:52 AM
I think I'm down to my last error code, but I can't seem to find the problem. This code works find in IE but I get an javascript error in NS

JavaScript Error:
file:/D:/My%20Documents/Bottomlee.com/Stationery%201.htm,
line 95:

can't convert x to an integer.

This is line 95:

else
{ // code for NS
butterfly.left = x; // this is line 95
butterfly.top = y;


A few lines before 95 I use this code to make sure that var x & y were integers


x = Math.round(R*Math.cos(A)+cx);
y = Math.round(R*Math.sin(A)+cy);


So I don't know why I'm getting this error. Can someone help me

Photo Joe

The complete code for the page is below:


<HTML>
<HEAD>

<STYLE TYPE="text/css">
body {
font-family: "Comic Sans MS";
font-size: 16pt;
margin-left: 420;
background-color: #000000;
color: #F0F0FF;
}

</STYLE>
<title></title>
<bgsound src="Midi/4marys.mid" balance="0" volume="0" loop="infinite">
<STYLE TYPE="text/css">
<!--

#butterfly {position:absolute; top: 0px; left:0px;}

-->
</STYLE>


<!-- An optional background image can be used but it will not scroll -->
</HEAD>

<BODY OnLoad= "window_OnLoad()"
<!-- This is the object in orbit. You should include the height and width background="images/Other/AK306.jpg" -->
<SPAN id=butterfly>
<img name = "pic" border="0" src="images/Other/monarch3_flap_md_clr.gif" width="70" height="80" style="position: absolute; top: 0; left: 0;">
</SPAN>

<SCRIPT language="JavaScript">
// Edgar V. Poirier
// moomoo@nbnet.nb.ca
// Dim w, wW, wH, myTimer, A, R, cx, cy, x, y

var w=document.body;
var myTimer = null;
var w;
var wW;
var wH;
var pic = document.images["pic"];
var A = 0, R = 0, cx = 0, cy = 0, x = 0, y = 0
if(document.layers)
{
var butterfly = document.layers["butterfly"]; // set var for NS object for butterfly gif
}

// Initialize
function window_OnLoad()
{
// Get Window dimensions
wW = screen.width;
wH = screen.height;
// Set the Radius;
R = wH/5;

A = 0; // Starting angle. Angles are in Radians. 1 degree = 3.14150/180 radians
// Set the center of the circle to 1/3 quadrant of the window
// Note the offset due to the fact that the object is
// positioned using its TOP and LEFT properties.
if (!document.layers) // test for browser type true =IE false = NS
{ // code if IE
cx = wW/3 - pic.width;
cy = wH/3 - pic.height;
}
else
{ // code for NS
var butterfly = document.layers["butterfly"]; // set var for NS object for butterfly gif
cx = wW/3 - butterfly.width;
cy = wH/3 - butterfly.height;
}
orbit();
}
// Move the object in a circle starting at an angle of 0, then increasing the angle.
function orbit()
{
clearTimeout(myTimer);
// Increase the angle
A=A+.01;
// Calculate the new coordinates
x = Math.round(R*Math.cos(A)+cx);
y = Math.round(R*Math.sin(A)+cy);

// Position the object.
if (!document.layers) // test for browser type true =IE false = NS
{ // code if IE
pic.style.left=x;
pic.style.top=y;
}
else
{ // code for NS
butterfly.left = x; // this is line 95
butterfly.top = y;
}
// Repeat for next angle change.
myTimer=setTimeout("orbit()",20);
}


</SCRIPT>
<p></p>
</BODY>
</HTML>

joh6nn
07-01-2002, 10:31 AM
well, i don't have NS4, so i can't help you there, but i do have a couple of things for you.

first, if you didn't know, NS6 no longer has document.layers. that shouldn't be much of a concern for you though; your code seems to be working fine in Mozilla.

second, what you assign to the style.left and style.top properties, are supposed to be strings, in the form of "#units", eg, "50px" or "2em". i don't know if that also holds true for NS4 layers. probably not, since it's having trouble converting to an integer.

as a suggestion on how to trouble shoot this, try sticking an alert in your else clause, so you can see what x really is. eg, alert(typeof x); will show if x is a "number", "string", or something else.

good luck with it.

PhotoJoe
07-01-2002, 02:59 PM
Ok I guess I should have added this information before.

I did put an alert in just before the error line and it showed that both x and y = NaN. I'm not sure what that means but it sure is not an interger. I had also put in an alert message just after the x = Math.round(R*Math.cos(A)+cx); and it show as an interger in IE but as NaN with NS.

joh6nn
07-01-2002, 04:20 PM
NaN is "Not a Number". probably, what's happening, is that somewhere along the line, you're doing something that NS4 is interpreting as a string usage of a variable, and not a numeric usage of a variable. you'll have to go through and check real carefull (using the typeof operator) to check and see if things are strings, or numbers.

my suggestion is to start with the screen.width and screen.height properties, and the width and height properties of the picture, as i'm not sure if those are string or numeric representations. if they're strings, that could be your problem.

PhotoJoe
07-05-2002, 05:45 AM
Thanks Joh6nn.

After using a lot of alert()s looking at the values of different variables and different points in the code, I found out that butterfly.height & butterfly.width were not a vaild element of the butterfly object array. I had to chage the code as shown below.

if (!document.layers) // test for browser type true =IE false = NS
{ // code if IE
pic.style.left=x;
pic.style.top=y;
}
else
{ // code for NS
butterfly.left = x;
butterfly.top = y;