etsted Posted March 26, 2014 Share Posted March 26, 2014 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 More sharing options...
thescientist Posted March 26, 2014 Share Posted March 26, 2014 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 More sharing options...
etsted Posted March 27, 2014 Author Share Posted March 27, 2014 whats the diffrence between the value they get? Can u write it down? Link to comment Share on other sites More sharing options...
Ingolme Posted March 27, 2014 Share Posted March 27, 2014 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(; // Shows 5alert(c); // Shows function() { return 5; } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now