javascript - What is the best practice to not to override other bound functions to window.onresize? -
how dig javascript, find myself asking much. example have window.onresize
event handler , if say:
window.onresize = resize; function resize() { console.log("resize event detected!"); }
wouldn't kill other functions connected same event , log message in console?
if think there should notifier tells me window resize event - or workaround maybe - without overriding other functions bound same event.
or totally confused usage?
you can save old onresize function , call either before or after custom resize function. example should work this:
var oldresize = window.onresize; function resize() { console.log("resize event detected!"); if (typeof oldresize === 'function') { oldresize(); } } window.onresize = resize;
this method can have issues if there several onresize functions. save old onresize function part of closure , call old 1 after function.
function addresizeevent(func) { var oldresize = window.onresize; window.onresize = function () { func(); if (typeof oldresize === 'function') { oldresize(); } }; } function foo() { console.log("resize foo event detected!"); } function bar() { console.log("resize bar event detected!"); } addresizeevent(foo); addresizeevent(bar);
when call addresizeevent, pass function want register. takes old resize function , stores oldresize. when resize happens, call function , call old resize function. should able add many calls like.
in example, when window resizes, call bar, foo, whatever stored in window.resize (if there anything).
Comments
Post a Comment