Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  109] [ 3]  / answers: 1 / hits: 170176  / 15 Years ago, thu, april 23, 2009, 12:00:00

I'm wondering if there's a way to count lines inside a div for example. Say we have a div like so:



<div id=content>hello how are you?</div>


Depending on many factors, the div can have one, or two, or even four lines of text. Is there any way for the script to know?



In other words, are automatic breaks represented in DOM at all?


More From » html

 Answers
20

I am convinced that it is impossible now. It was, though.



IE7’s implementation of getClientRects did exactly what I want. Open this page in IE8, try refreshing it varying window width, and see how number of lines in the first element changes accordingly. Here’s the key lines of the javascript from that page:



var rects = elementList[i].getClientRects();
var p = document.createElement('p');
p.appendChild(document.createTextNode(''' + elementList[i].tagName + '' element has ' + rects.length + ' line(s).'));


Unfortunately for me, Firefox always returns one client rectangle per element, and IE8 does the same now. (Martin Honnen’s page works today because IE renders it in IE compat view; press F12 in IE8 to play with different modes.)



This is sad. It looks like once again Firefox’s literal but worthless implementation of the spec won over Microsoft’s useful one. Or do I miss a situation where new getClientRects may help a developer?


[#99656] Friday, April 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;