Jump to content

dont understand


etsted
 Share

Recommended Posts

this is an animation script that works fine, but i notize that the init() function only works if the brackets are not included. Why?

 

<html><head><title>JavaScript Animation</title><script type="text/javascript"><!--var imgObj = null;function init(){ imgObj = document.getElementById('myImage'); imgObj.style.position= 'relative'; imgObj.style.left = '110px';}function moveRight(){ imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';}window.onload =init ; // works

window.onload = init(); // doesnt work//--></script></head><body><form><img id="myImage" src="smiley.gif" /><p>Click button below to move the image to right</p><input type="button" value="Click Me" onclick="moveRight();" /></form></body></html>

Link to comment
Share on other sites

right, that's how event handlers work. you assign them a reference to function. if you call the function, like in your second example, then you are just assigning the return value of that function to the event handler.

Link to comment
Share on other sites

OK, here's the same situation with different variables and functions. In this case, the function actually returns a value

function a() {    return 5;}var b = a();var c = a;alert(B); // Shows 5alert(c); // Shows function() { return 5; }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...