Scrolling textarea in Opera

B

bwucke

Is there any way to scroll textarea (to the bottom row) from Javascript
in Opera? The

textarea.scrollTop=textarea.scrollHeight+textarea.scrollTop;

approach doesn't work, and there's so many notices that it doesn't all
over the net, that if there's any solution/workaround to this problem
on the net, it's unfindable.

Alternatively (my workaround) is there any way to reliably count lines
of text in Textarea so I could crop the start of the text so all of it
fits if more is added? (note: newlines, word wrap, tabs and optionally
variable size fonts should be taken into account. My current approach
is to split text at newlines and count each as
Math.ceil(line.length/textarea.cols) textarea lines but it doesn't take
into account long words triggering faster line wraps, tabs etc so
sometimes I overflow and bottom line gets hidden as more text is added.
 
B

bwucke

One more quirk I recalled. <textarea cols=120; style="width: 20em;">
etc. Counting lines requires reliably counting columns and it seems
stylesheets can override the cols= parameter. I'd prefer my page to be
scalable and at least gracefully accept font resizing without creating
horizontal scrollbar on the browser window. Sorry for replying to self.
 
G

Gérard Talbot

Followup-to set: comp.lang.javascript

Is there any way to scroll textarea (to the bottom row) from Javascript
in Opera? The

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.
approach doesn't work,

It never will in any browser. You're misunderstanding these 2
properties. You're mishandling these 2 properties.

http://www.gtalbot.org/BugzillaSection/DocumentAllDHTMLproperties.html
http://www.mozilla.org/docs/dom/domref/scrollHeight.html
http://www.mozilla.org/docs/dom/domref/scrollTop.html
http://developer.mozilla.org/en/docs/DOM:element.scrollHeight
http://developer.mozilla.org/en/docs/DOM:element.scrollTop


Opera 7, Opera 8 and Opera 9 do not render the css width and css height
correctly for textarea: it's wrongly implemented.
Bug 40 at this page:
http://www.gtalbot.org/BrowserBugsSection/Opera9Bugs/
https://bugzilla.mozilla.org/attachment.cgi?id=117564

and there's so many notices that it doesn't all
over the net, that if there's any solution/workaround to this problem
on the net, it's unfindable.

Alternatively (my workaround) is there any way to reliably count lines
of text in Textarea so I could crop the start of the text so all of it
fits if more is added? (note: newlines, word wrap, tabs and optionally
variable size fonts should be taken into account. My current approach
is to split text at newlines and count each as
Math.ceil(line.length/textarea.cols) textarea lines but it doesn't take
into account long words triggering faster line wraps, tabs etc so
sometimes I overflow and bottom line gets hidden as more text is added.

You posted no url to show what you're proposing.

One last note: opera.beta is a newsgroup for testing with/commenting
on/reporting problems in/about beta releases.

Followup-to set: comp.lang.javascript

Gérard
 
B

bwucke

Gérard Talbot said:
Followup-to set: comp.lang.javascript



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.
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.
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.length + 8.0) / cols );
}
return count;
}
 
G

Gérard Talbot

It just scrolls down by scrollHeight minus height.

Ok, so why don't you use this calculation instead? If you know this is
the correct code, then why not use it? What's wrong with using the
correct code to do this?
True, plain textarea.scrollTop=textarea.scrollHeight; would suffice.

It works but it's still a wrong calculation. scrollTop value is never
the scrollHeight value; never.
Have you tried? It works in MSIE and Firefox/Mozilla family just fine.

It is a wrong calculation. You are relying on error correction mechanism
here. Next week, next month, next year, whatever, browser manufacturers
may change the error correction mechanism and report an error in the
javascript console. Your code is not correct and relies on a specific
error correction mechanism.
Try entering "400" in the fine document you've provided. :
http://www.mozilla.org/docs/dom/domref/scrollTop.html

Try entering 1000: it's still wrong.
Writing -anything- to scrollTop in Opera does nothing. Values well
within allowed range are disregarded just as well.




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.




function out(txt) /* Write a string to output textarea. */

You pasted just functions: I was expecting an url showing the problem or
what you were proposing.

Gérard
 
B

bwucke

Gérard Talbot napisal(a):
Ok, so why don't you use this calculation instead? If you know this is
the correct code, then why not use it? What's wrong with using the
correct code to do this?

How much is 400px - 8em ?
(as I mentioned, my page is scalable.)
Try entering 1000: it's still wrong.

which doesn't mean it doesn't work. And since scrollTop is not a part
of any standard or recommendation, its support may cease in any new
browser revision altogether, so by your reasoning using it at all is
wrong. I'm depending on safety mechanisms of poorly docummented and
unstandarized feature. I'm fully aware it's stepping on thin ice,
that's why I asked for potentially better solution.
You pasted just functions: I was expecting an url showing the problem or
what you were proposing.

if you insist...

http://www.kurs.horsesport.pl/inne/testcase.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top