473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

determining 1st line of text

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>
Jul 20 '05 #1
2 2025
Yep
"Csaba2000" <ne**@CsabaGabor.com> wrote in message news:<bi********@dispatch.concentric.net>...
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.
Jul 20 '05 #2
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

"Yep" <y-*******@em-lyon.com> wrote in message news:d2**************************@posting.google.c om...
"Csaba2000" <ne**@CsabaGabor.com> wrote in message news:<bi********@dispatch.concentric.net>...
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.

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Abrahams | last post by:
Is there any way to determine the file and line number (if any) of a method's invocation from within its body? Many Thanks in advance, Dave -- David Abrahams Boost Consulting...
2
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as...
1
by: Simon Wigzell | last post by:
I am adapting a javascript pulldown menu system to my dynamic website generator - the arrays that hold the menu items information are read from a database and will be different for different users...
2
by: DMD | last post by:
How do I determine the column associated with the current edit location within a RichTextBox? The following code allow me to determine the current line txtLineNumber.Text =...
1
by: blue | last post by:
I have a table that I want to have a set width and height. The table has a title and the text may be larger than the width of the table. If the text is too large to fit into the table, it either...
3
by: Jozef | last post by:
Hello, I'd like to determine the size of a file via a URL. I'm not even sure how to do this on the local machine, let alone a URL. Any help would be greatly appreciated. Thanks!
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
1
by: sirrobert | last post by:
Hey everyone. I've got some DIV elements containing arbitrary HTML that is entirely text (some of the childNodes are textNodes, some are strong or em tags containing textNodes, some are divs or spans...
3
by: Gord | last post by:
If I have a form open with a subform control on it in datasheet view that has its record source set to a query or a table, is it possible to determine which record the user has clicked into with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.