I want to call a function onclick and pass the event object to the function:
progressBarOuter.onclick = function(e) {
var x; if (!e) {
x=window.event.clientX;
}
else {
x = e.clientX
}
yt.clickedOffset = x; yt.progressBarClicked
}
So i’m assigning clickedOffset to by enclosing object (yt) and then calling progressBarClicked which then uses the clickedOffset var. But what I actually would rather be doing is something like this:
progressBarOuter.onclick = yt.progressBarClicked(e);
Because it’s much more compact. Problem is that even if the user has clicked or not this bit of code is executed… yt.progressBarClicked(e).
Is there any way around this?
Your:
doesn’t yield the expected result, because you’re not assigning the function
yt.progressBarClickedto theonclickhandler. You are actually calling the function and therefore assigning its return value toonclick.The shortest working thing you can get without breaking anything is this:
If you don’t wrap it,
yt.progressBarClickedwill be called from thewindowobject and therefore the value ofthisinside the function will be also set to thewindowobject.Of course you could also handle the calculation of the x position inside the function and just pass the event to it: