in your HTML function remainingwidthheight (dx, dy, id) { var list, k, el, sel, s, flag, t; var vw, vh, w, h, hpos, vpos; var dx1, dy1; console.log('remainingwidthheight:', 'dx=', dx, 'dy=', dy, 'id=', id); if (!(id)) { el = getelement('script'); dx1 = null; dy1 = null; s = window.getComputedStyle(el); if(dx) { if (s.minWidth == "1px" ) dx1 = dx else el.style.width = "auto"; }; if(dy) { if (s.minHeight == "1px" ) dy1 = dy else el.style.height = "auto"; }; if ((dx1) || (dy1)) { if (el.id) id = el.id else { id = "ID_" + uniqueId(); el.id = id; console.log('id generated:', id); }; t = 100; setTimeout(function () { remainingwidthheight(dx1, dy1, id); }, t); listenresize(function(){ remainingwidthheight(dx1, dy1, id) }); console.log(dx, dy, id, "resize listener set"); flag_remainingheight = 1; }; return; }; console.log('remainingwidthheight:', id); el = document.getElementById(id); var vw = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var vh = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; console.log('viewport:', vw, vh); var hpos = el.offsetLeft; var vpos = el.offsetTop; var w = vw - hpos; var h = vh - vpos; console.log('remaining w,h:', w, h); if(dx) { dx=dx-0; console.log('dx:', dx); if ((dx < 1) && (dx> -1)) w = w + Math.round(dx * vw) else w = w+dx; console.log('w:', w); el.style.width = w + "px"; console.log('id:', id, 'width set:', el.style.width); }; if(dy) { dy=dy-0; console.log('dy:', dy); if ((dy < 1) && (dy> -1)) h = h + Math.round(dy* vh) else h = h+dy; console.log('h:', h); el.style.height = h + "px"; console.log('id:', id, 'height set:', el.style.height); }; }; var uniqueId = (function(){var id=0;return function(){if(arguments[0]===0)id=0;return id++;}})(); function listenresize (myfunc) { console.log("listenresize", myfunc); if (window.addEventListener) window.addEventListener("resize", debounce(myfunc, 500)); else if (window.attachEvent) window.attachEvent("resize", debounce(myfunc, 500)); else window.onresize = function() {debounce(myfunc, 500)}; }; debounce = function(myfunc, wait) { var timeout, args, context, timestamp; return function() { context = this; args = arguments; timestamp = Date.now(); console.log("debounce:", timestamp, args); var timeoutfunc = function() { var tnew = Date.now(); var tdif = tnew - timestamp; console.log(tnew, timestamp, tdif, timeout); if (tdif> wait) { timeout = null; console.log("run func",args); myfunc.apply(context, args); } else { timeout = setTimeout(timeoutfunc, wait - tdif); console.log("timeout updated", wait, timeout); }; }; if (!timeout) { timeout = setTimeout(timeoutfunc, wait); console.log("debounce: timeout set", wait, timeout); }; }; };