Jump to content

Return a value: how can a function do this?


tinfanide

Recommended Posts

var evt, delta, d;function show(e){evt = window.event || e;delta = (evt.detail) ? evt.detail*(-120) : evt.wheelDelta ;delta = (delta>0) ? delta/delta : (delta/delta)*-1 ;return delta;}var move = (!document.all) ? "DOMMouseScroll" : "onmousewheel" ;if(document.attachEvent){document.attachEvent(move,show);}else if(document.addEventListener){document.addEventListener(move,show,false);} window.onload = function(){console.log(show());}

I want the show function to return the mousescroll value to be used in another function.But it returns evt being undefined (in the debugger).

Link to comment
Share on other sites

This part isn't going to give any data:

window.onload = function(){console.log(show());}

The event object only has data when the event fires. Use console.log() inside the show() function and look for data when the mouse wheel scrolls.

Link to comment
Share on other sites

<!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"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>var evt = (!document.all) ? "DOMMouseScroll" : "onmousewheel" ;if(document.attachEvent){document.attachEvent(evt,func);}else if(document.addEventListener){document.addEventListener(evt,func,false);} //if(document.attachEvent){document.attachEvent(evt,f);}else if(document.addEventListener){document.addEventListener(evt,f,false);}//function func(evt){if(!evt){evt = window.event}// evt = window.event || e;delta = (evt.detail) ? evt.detail*-120 : evt.wheelDelta;return delta;}function f(evt){alert(func());}</script></head><body></body></html>

I have attached the f function to the document. It works in IE but in FF9, it says evt is undefined.

Link to comment
Share on other sites

It's the exact same problem. The function that takes the event object needs to be the function that you tell it to run when the event happens. You're trying to use an event object in "func", but then you're telling it to run "f" when the event happens. So f gets the event object, not func. The first event handlers will work when you tell it to run func, but not when you tell it to run f.

Link to comment
Share on other sites

It's the exact same problem. The function that takes the event object needs to be the function that you tell it to run when the event happens. You're trying to use an event object in "func", but then you're telling it to run "f" when the event happens. So f gets the event object, not func. The first event handlers will work when you tell it to run func, but not when you tell it to run f.
I'm not sure if I have understood from your words or not:
var evt = (!document.all) ? "DOMMouseScroll" : "onmousewheel" ;if(document.attachEvent){document.attachEvent(evt,f);}else if(document.addEventListener){document.addEventListener(evt,f,false);}function func(e) // the parameter "e" is passed to the "e" below{    evt = window.event || e; // this "e" is used by Firefox    delta = (evt.detail) ? evt.detail*-120 : evt.wheelDelta;    return delta;}function f(e){alert(func(e));}

In my previous not working codes, the parameter in func() is not the same to the evt variable (e);Andin the function f, the event should be passed to the function func within the function f. Anyway, the above codes should work in both IE and FF.

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
×
×
  • Create New...