determining 1st line of text

C

Csaba2000

How can I determine the first displayed
line (or nth line, for that matter) of text
in an a given element? It can be assumed
that the element has a fixed width (such
as a DIV in a TD with a fixed column width).

Thanks for any tips,
Csaba Gabor from New York
I'm on IE 5.5

e.g.
<BODY onLoad="alert('The first line is: '+calcLine(1))">
<SCRIPT language=javascript type='text/javascript'>
function calcLine(lineNum) {
return ("line " + lineNum + " needs work")
}
</SCRIPT>
<TABLE>
<TR>
<TD style="width:1in"><DIV>
This line is so long that it will wrap a few times
</DIV></TD>
</TR>
</TABLE>
</BODY>
 
Y

Yep

Csaba2000 said:
How can I determine the first displayed
line (or nth line, for that matter) of text
in an a given element?

In IE that's pretty easy with ranges, since IE provides access to
ranges' dimensions (see generic example below). In W3C terms I don't
think this is doable, ranges and selection are clearly related to
nodes, not style.


<div onclick="lineLength(this)" style="position:absolute;width:5em">
This is a very long line and it's about to rain outside.
</div>

<script type="text/javascript">
function lineLength(obj){
var d=document;
if(d.body && d.body.createTextRange){
var rng=d.body.createTextRange(), refHeight;
rng.moveToElementText(obj);
rng.collapse(true);
refHeight=rng.boundingHeight;
while(refHeight==rng.boundingHeight)
rng.moveEnd("word",1);
rng.moveEnd("word",-1);
//first line in rng.text
rng.select();
}
}
</script>


HTH
Yep.
 
C

Csaba2000

Yep

thank you, Thank You, THANK YOU!

Not only did your solution work right out of the box,
but I haven't used TextRange before so I learned an
awful lot from your compact example. Cool.

Csaba
 

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,007
Latest member
obedient dusk

Latest Threads

Top