JavaScript Self Invoking Function on addEventListener -
i'm not being able make function of event listener self invoke , listener work.
the following code executes function, event listener don't work:
window.addeventlistener("resize", (function () { document.getelementbyid("divmenu").innerhtml = document.getelementbyid("divtop").offsetwidth })())
the function set needed (dynamic since beginning) css style essential website formatting. "resize" function must executed @ load. possible make this, or should create separate self invoking function , call on event listener?
when invoke function, it's return value put in it's place (window.addeventlistener('resize', undefined)
). instead, define function outside of event listener add , call it.
function onresize() { document.getelementbyid('divmenu').innerhtml = document.getelementbyid("divtop").offsetwidth; } window.addeventlistener('resize', onresize); onresize();
technically, make work using self-invoking function it's bit screwy , wouldn't suggest it.
window.addeventlistener('resize', (function onresize() { document.getelementbyid('divmenu').innerhtml = document.getelementbyid("divtop").offsetwidth; // works because returns function return onresize; })());
Comments
Post a Comment