September 11, 201214 yr Hey, I am playing around with some javascript stuff in "Javascript the definitive guide" and I have this little piece of code : <script> function find_browser_size() { var elt = document.getElementById('results_div'); var cords = elt.getBoundingClientRect(); var findWidth = cords.width; var findHeight = cords.height; document.write('the current width of the div is:' + findWidth + ' pixels wide.' +'<br/>'); document.write('the current height of the div is:' + findHeight + ' pixels high.'); } find_browser_size(); </script> All it is doing if finding the size of the window and outputting the result to the screen. If I resize the window and refresh, it updates but I want it to constantly check to see if the browser has been resized, so that the results change as soon as the window is resized. How would I do this? Many thanks for any help
September 11, 201214 yr Try this: <html> <style type="text/css" > * { padding:0px; margin:0px; } body , html { width:100%; height:100%; } </style> <body onresize='resize()' > <div id="holder" ></div> <script type="text/javascript"> function resize() { var bodyWidth = window.getComputedStyle(document.body,null).getPropertyValue('width'); var bodyHeight = window.getComputedStyle(document.body,null).getPropertyValue('height'); document.getElementById('holder').innerHTML = 'Width: '+bodyWidth + ' Height: '+bodyHeight; } </script> </body> </html> Notes: Not tested in IE. Dosen't work if body element is bigger then the height of the browser, because it actually returns the height of the body element, and not the available height of your browser.
September 11, 201214 yr Ah! Thats exactly what I was looking for - Thank you very much You're welcome
September 11, 201214 yr Better yet, don't mix Javascript with mark up. You should avoid this: <body onresize='resize()' > And stick to the following within the <script> tags: window.onresize = resize;
Create an account or sign in to comment