Gérard Talbot wrote:
Followup-to set: comp.lang.javascript
bw****@gmail.com wrote : textarea.scrollTop=textarea.scrollHeight+textarea. scrollTop;
The scrollTop value can never be equalt to the scrollHeight: this is
impossible.
So to add the current scrollTop value to scrollHeight is even more
impossible.
It just scrolls down by scrollHeight minus height.
True, plain textarea.scrollTop=textarea.scrollHeight; would suffice.
approach doesn't work,
It never will in any browser. You're misunderstanding these 2
properties. You're mishandling these 2 properties.
Have you tried? It works in MSIE and Firefox/Mozilla family just fine.
Try entering "400" in the fine document you've provided. :
http://www.mozilla.org/docs/dom/domref/scrollTop.html
Writing -anything- to scrollTop in Opera does nothing. Values well
within allowed range are disregarded just as well.
Opera 7, Opera 8 and Opera 9 do not render the css width and css height
correctly for textarea: it's wrongly implemented.
Thanks. Little in common with this problem (as I mentioned, no matter
what, scrollTop is ignored), but at least I won't bother with these
while calculating number of lines.
Alternatively (my workaround) is there any way to reliably count lines
You posted no url to show what you're proposing.
function out(txt) /* Write a string to output textarea. */
{
document.f.out.value += txt;
//the following is broken in Opera.
document.f.out.scrollTop = document.f.out.scrollHeight;
if(Opera){ // defined elsewhere
document.f.out.value=crop(document.f.out.value);
}
}
function crop(txt) /* We can't scroll textarea in Opera, */
{ /* so let's at least make the text fit */
while( countlines(txt) >= document.f.out.rows){
txt=txt.slice(10); // remove 10 chars from front, try again
};
return txt
}
/* estimating the number of lines txt takes in textarea. */
function countlines(txt)
{
var cols=document.f.out.cols;
var count=0;
var ln=String(txt).split('\n');
for(var i=0;i<ln.length;i++){
/* each logical line takes at least one physical line.
* Every /cols/ chars is a physical line, and a tab is 8 chars.
* tabs are common and we'd better arrive at more than less what
* really is displayed. */
count += Math.ceil( (ln[i].length + 8.0) / cols );
}
return count;
}