How much text fits in the frame?

H

Hank Mishkoff

Is there any way to use JavaScript to determine how much
text will fit into a non-scrolling browser frame?

I have a non-scrolling frame in which I want to insert some
text. I know that the text is too large to fit entirely into
the frame, so I'd like to insert only the portion of the
text that will fit into the frame. Is there any way to use
JavaScript to do this?

(The text could be any font and any size, and the frame
could be any size as well, though not resizable.)

If so, my second task is to put the remaining text into a
second frame, which means that I not only want to insert
into the first frame only the text that fits, I need to know
exactly what text was put into the first frame, which will
allow me to identify the remaining text and put it into the
second frame.

Is any of this possible with JavaScript? If not, is there
*any* way to do this?

Thanks for your help!
 
H

Hank Mishkoff

I don't know if this will make a difference, but I should
mention that this has to work under IE7 only.
 
J

Jonas Raoni

Hank Mishkoff escreveu:
Is there any way to use JavaScript to determine how much
text will fit into a non-scrolling browser frame?

I have a non-scrolling frame in which I want to insert some
text. I know that the text is too large to fit entirely into
the frame, so I'd like to insert only the portion of the
text that will fit into the frame. Is there any way to use
JavaScript to do this?

If the font was monospaced you could calculate it easily :]

Well, I'm all for calculating a generic size and crop the remaining
text, letting the layout stretch if needed... Like using a "word wrap"
function. But it's not what you want...

The best solution in my opinion is to calculate the required size on the
server, but since you'll need a lot of informations (distance between
each line, between characters, font name, size, bold, italic), it will
be quite annoying.

Doing it on the client side I think it's very bad, since I don't know
anything automatic like element.getTextOffsets(text). But if it's really
needed, you can: clone the node; insert it in the same place of the
cloned node; move it to a invisible place (position: absolute, left: -
node.offsetWidth, etc.); calculate a generic crop point, then add/remove
characters until the scrollHeight satisfies the scrollTop property.
 
L

Laurent Bugnion

Hi,

Hank said:
Is there any way to use JavaScript to determine how much
text will fit into a non-scrolling browser frame?

With Java, it's possible to calculate the width of a given string with a
given font. With JavaScript, however, it's not easy. One possible
solution is (was...) to call the corresponding Java function from
JavaScript (in browsers supporting it), but I wouldn't recommend that,
because Java might not be installed, the JVM is very slow to start, etc...

Better solution is to simply put the whole string in, and then use CSS
to hide what's too long, using the CSS "overflow" property.

http://www.w3schools.com/css/pr_pos_overflow.asp

HTH,
Laurent
 
H

Hank Mishkoff

Better solution is to simply put the whole string in, and then use CSS
to hide what's too long, using the CSS "overflow" property.

If I do that, is there any way to determine what's actually
been displayed? I'd like to be able to display the portion
that "overflowed" in another frame, but I can't do that
unless I know exactly what was displayed in the first frame.
 
L

Laurent Bugnion

Hi,

Hank said:
If I do that, is there any way to determine what's actually
been displayed? I'd like to be able to display the portion
that "overflowed" in another frame, but I can't do that
unless I know exactly what was displayed in the first frame.

Unfortunately, no, there isn't.

Greetings,
Laurent
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top