JavaScript Trouble, Please help

They have: 13 posts

Joined: May 2005

Greetings,

Ok. I think there is a problem with my javascript. This script is suppose to create a spinning box. Can you please help me?

<script type="text/javascript">
// global variable to store position
var pos = 0;

// function to rotate a div with id of "d1"
function spin()
{
// get the div's style object
var obj_style = document.getElementById("d1").style;
// store desired radius and x,y offsets
var radius = 40;
var x_offset = 50;
var y_offset = 50;
// increment to the new position
pos += 10;
// use Math to calculate the new x.y coordinates
x = radius * Math.cos( pos * Math.PI/180 );
y = radius * Math.sin( pos * Math.PI/180 );
// add the x, y offsets
x += x_offset;
y += y_offset;
// move the div to its new coordinates
obj_style.left = x + "px";
obj_style.top = y + "px";
// set the interval to call the function again
setTimeout( "spin()",100 );
}
</script>


</head>
<body onload="spin()">
'

I had got this code / script from a JavaScript book... Can someone please help me with it. Thanks. Laughing out loud

They have: 5,633 posts

Joined: Jan 1970

1) what error are u getting
2) try not to use free scripts

They have: 13 posts

Joined: May 2005

Greetings,

1. What error are you getting?
-- None. Nothing appears.
2. Try not to use free scripts.
-- I didnt. Well sort of, but I coded some of it by myself. I got it from a JavaScript book, (part of it) and I coded the rest.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Code works for me in Firefox. Had to add a d1 element myself, though.

What does your d1 element look like? What browser are you using?

Smiling

They have: 13 posts

Joined: May 2005

thats my problem i guess... what do i have to use for the d1 ?? can you tell me or help with this Laughing out loud:D Also, how would I just draw a plain simple red box ?? I would like to make that spin Laughing out loud

chrishirst's picture

He has: 379 posts

Joined: Apr 2005

If you don't have an element with an id of "d1", nothing will happen because the script won't find anything to spin.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

This is what I used to test it:
<div id="d1" style="position:absolute; left:100; top:100; width:100; height:100; border: 1px solid black"></div>'

It's just a 100x100 div, positioned absolutely. You can use whatever you like... I can't really tell you what you want. Smiling Just make sure it has id="d1".

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.