Forum Moderators: open

Message Too Old, No Replies

Help with Moving a div

Error: divPath has no properties

         

aeve

7:31 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



I'm extremely unable when it comes to javascript so it's probably something very simple I'm missing. A client wants me to have an image of a bird move when the page loads. The javascript and css are both in external files and for now it only needs to work in DOM browsers. I'm getting the javascript error:
Error: divPath has no properties
from line 5 and no movement and I can't figure out why it's not picking up the properties. Here's the code:

javascript:


function moveIt(id,x)
{var supported = (document.getElementById);
if (!supported) return;
var divPath = document.getElementById(id);
var currPos = parseInt(divPath.style.left);
if (currPos < x)
{currPos += 5;
divPath.style.left = currPos;
setTimeout("moveIt('" + id + "'," + x+ ")",10);
}
}
window.onload = moveIt('birdy',300);

css:

#birdy {position: absolute; top: 275px; left: 34px; width: 265px; height: 248px; background: url(images/bird.png) no-repeat; }

html:

<body id="splash">
<div id="wrap"><div id="content">
...
<div id="birdy">&nbsp;</div></div></div>
</body>

Any help would be greatly appreciated.

ajkimoto

7:53 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



aeve,

Your problem is that the js function is attempting to get the style.left of the birdy div when you have not directly assigned a 'left' to the div. Rather you have applied a style to the birdy div. As far as js is concerned, there is no style.left assigned to the birdy div unless you explicitly assign one.

If you put style="left:34px" in the birdy div itself, your function should work fine.

Alternately, you can use the currentStyle attribute for IE and the getComputedStyle attribute for Moz.

Hope this helps,

ajkimoto

Bernard Marx

8:29 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not adding the units (px) to the value may get you into trouble with stricter browsers:

divPath.style.left = currPos + "px"

The delay of 10ms is faster than you need too possibly.

ajkimoto

9:15 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



Bernard,

You are absolutely correct on the 'px' issue. I omitted that for clarity.

I have used this in the past:

if we assume that the value of height='20px':

var tempHeight=height.split('p')
var actualHeight=tempHeight[0]

ajkimoto

aeve

10:59 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



Thanks for the help, I'm sure those are both problems I would've eventually run into -- but I think my problem comes earlier. It seems like 'birdy' isn't getting picked up at all by the function. When I try to return
getElementById(id)
I get "null".

I'm doing a little server-side scripting on the page (mainly to serve nn4) the output of the <head> ends up looking like this:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Home</title>
<meta name="Description" content="" />
<script type="text/javascript" src="bird.js"></script>
<link rel="stylesheet" href="birdy.css" type="text/css" media="all" />
<meta http-equiv="imagetoolbar" content="no" />
</head>

Could the order in which things are being called be causing the script to run before the div gets into the DOM or something like that?

One day this bird is gonna fly.

Bernard Marx

11:18 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. It's the onload assignment itself:

window.onload = moveIt('birdy',300);

That will assign window.onload to the value returned by the function - not what we want. It also calls the function immediately (not onload), before 'birdy' exists. Change it to:

window.onload = function(){ moveIt('birdy',300)};

aeve

11:34 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



It Flys!
Thanks a lot.

Adam

Bernard Marx

11:47 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool.