Chrome (and IE when browsing local files) gives an initial value for the clientHeight of a textarea element that is incorrect. How do I get IE and Chrome to give the correct values? (Problem demo'ed here)
I am setting the position and size of the textarea via CSS fixed/absolute positioning. Then I am checking the value of clientHeight via JavaScript/jQuery.
The problem does not happen all the time, but is consistently reproducible. Specifically, the following conditions are required:
- Using Chrome (5.0). (Also IE (8.0) if the file is opened from the local computer.)
- Browser window is maximized.
- jQuery is included inline (vs. linked as an external file)
- The page has not been loaded since the browser was started.
- My app has not processed any commands, yet.
- The variable paddingLines is dynamically determined based on clientHeight. (Setting to 50 every time is a work-around.)
I need accurate clientHeight and scrollHeight values because I use these properties to determine where to 'snap' the textarea scrollHeight to so the last output starts on the first line of the text area. Actually, if the properties were consistently inaccurate between calls it would still work. Unfortunately, the inaccurate properties are adjusted to their correct values by the time the first command is processed.
I have tried to isolate the problem in a demo here. When I tried to make an even simpler sample, the problem did not reproduce as consistently. I think there are some timing and/or browser optimization issues because the following work-arounds make the problem go away:
- Setting the variable paddingLines to 50 every time (instead of dynamically calculating it based on clientHeight)
- Including jQuery as an external file (instead of inline)
- Adding a short delay (as short as 2 milliseconds) before the textarea is initialized.
- Loading the app in IE from a server (vs. opening from the local computer)
However, these work-arounds are only kludges and I would prefer to find the true cause/solution to the problem. Although this is a minor aesthetic problem, I wish to gain a deeper understanding of how the Html DOM, CSS, Javascript, and jQuery work in different browsers. Is there something wrong with my code?
Notes:
- The causes for Chrome's inaccurate properties seem to be different than IE's
- Chrome's innaccurate clientHeight seems to be based on the non-maximized size of the browser.
- IE's innacurate clientHeight may be affected by the info bar warning about unsafe scripts.