View Full Version : Counting the number of lines in a file

03-06-2005, 07:24 PM
I have a file on my server that has a number of lines in it, depending on the number of users in the chat-room.

What I'd like to do is to count the number of lines in this file, so that I can display it next to the Link for opening the chat-room (i.e. 0 people chatting, 27 people chatting; and {if I can be bothered being clever} 1 person chatting).

However, I cannot find a javascript way of counting lines in a file. Any ideas?


Puffin the Erb
03-06-2005, 09:09 PM
You will need a server-side language to do that, client-side JavaScript cannot.

03-07-2005, 10:11 AM
Just for the sake of argument, if that's a text file which can be loaded into an iframe, it may be possible to get the line count indirectly...

For example, this works in IE:

<iframe style="display:none" src="xyz.txt" onload="alert(contentWindow.document.getElementsByTagName('PRE')[0].innerHTML.match(/\n/g).length + 1)"></iframe>

...unless those are nothing but blank-lines, because IE strips innerHTML--if its nothing but line-breaks. :D

03-07-2005, 10:43 AM
It's been pointed out that JavaScript isn't really the language that I should be using, so I've kinda gone for that iFrame approach, except, the iFrame src loads a .php rather than anything else. Positioning relative to the chat room, a teeny tiny iFrame just displays "($number)".

A bit inellegant, but never mind.

I like the idea of alerting the parent window through an iFrame, but would that work with FireFox?

Thanks for the help!


03-07-2005, 10:56 AM
Would it work in FF? I dunno, try it & see... :)