View Full Version : script for calculating # of lines used on a page

08-03-2004, 11:51 PM
i need to design a script to calculate the number of lines of a page that have been used. based on the result i want to be able to activate or not activate certain features.

what i've come up with so far is this:
to not lose any data on a printout of 8.5x11 inch letter size paper, you need:
width = 536 pixels
639 pixels high
(or 216 x 279 mm)
according to http://dsv.su.se/jpalme/web-ruler.html

how can i use this to calculate how far down i am with regards to nearing the end of a printable page?

Willy Duitt
08-04-2004, 04:06 AM
Here is the routine I use in my editor to line number when debugging scripts. It may help in answering your question on line numbers but I forsee a fatal flaw in your logic that line numbers will help you in determining where to break a page for printing...

The flaw being...
You will also need to find the line height in order to make a make a more accurate guesstimate....


<script type="text/javascript">
function lineNumber(form){
input = form.text.value;
input = input.replace(/\n\r/g, '\r');
input = input.replace(/\r\n/g, '\r');
input = input.replace(/\n/g, '\r');
line = input.split('\r');

for(var i=0; i<line.length; i++){
line[i] = (i*1+1)+': '+line[i];
} form.text.value = line.join('\r');

<textarea cols="90" rows="25" name="text"></textarea>
<br /><br />
<input type="button" value="Line Number" onclick="lineNumber(this.form);" />


08-04-2004, 06:37 PM
hehe, always on the ball. I realised the flaw aswell, which was going to be my next question.

assuming that all the font sizes were the same (say... font size = 2), could this be programmed into the script?

in retrospect i suppose i'm complicating things. basically what i'm trying to do is as follows:

if the printed page height and width are as follows:
height = 639 pixels
width = 536 pixels

i'd like to be able to say:

if ( height > 600 px )
class(static-box)= visibility: hide;

the reason for this is because i have a CSS class that displays "company info" on the bottom of a form success page so that when the people print out the page, it has the added info on the bottom of the page. as seen here

<style type="text/css" media="print">
.notPrinted { display: none; }
.static-box { display: block; }

<style type="text/css" media="screen, projection">

bottom of body:

<table border="0" align="center">
<td align="center" style="position:absolute; bottom: 0px; page-break-after:always;" class="static-box"><div align="center"><font size="-1">Enviromark Inc.<br />
www.enviromark.ca</font> </div></td>

the problem is as follows... if the user fills out a ton of info in the textarea, then this little table above displays ON TOP OF the text in the textare output.

i need the if statement above to basically say "if the page is used beyond 600px, do not print this table above, because it'll print on top of the text.

heh, i hope i explained it well / concisely :)

oh, P.S: should i move the style="position:absolute; bottom: 0px; page-break-after:always;" class="static-box" line to the <table> header? would it facilitate things?

08-06-2004, 05:58 AM
nothing, anyone?

08-11-2004, 05:52 PM
can anyone help me here?

all i need to do i get a little script to read the height in px that would be displayed on the screen, and do an if statement based on that...

08-11-2004, 06:03 PM
FYI: there's some good JS tutorials on printable pages at DocJS (http://www.docjs.com).

hope that helps a little :)

08-11-2004, 09:55 PM
that'd be great... however i cant seem to find anything on printing. any chance you could paste a direct url?

08-12-2004, 05:34 AM
Put the content in a div then get its offset height.

<div id="content">
content here
if (document.getElementById('content').offsetHeight > 600)
//hide footer

And you don't need to put the footer in a table. Just put it in a div tag.

<style type="text/css" media="screen, projection">
.static-box {
bottom: 0px;
<div class="static-box">Enviromark Inc.<br />www.enviromark.ca</div>

08-12-2004, 10:49 AM
mr jamieson:

>> Page Breaks in Printing (http://www.webreference.com/js/tips/000201.html)
>> Pixels to Inches Conversion (http://www.webreference.com/js/tips/011016.html)
>> Counting Pages in A Print Job (http://www.webreference.com/js/tips/010928.html)
>> Converting Dimensions for Printing (http://www.webreference.com/js/tips/010925.html)

these may not be wot you need or want, but they're worth a wee browse as is the rest of the site. :)

08-12-2004, 03:22 PM
good stuff, thx