PDA

View Full Version : How to Make Netscape and IE report the same Character Count using JavaScript


jbelmonte
01-30-2003, 09:08 PM
Hi,

I have been trying to write a cross-browser compatible character counting JavaScript function. In addition, I have looked at a number of "free" scripts; however, my script, like every other script I have been able to locate, provides different numbers for the same text when Netscape is used instead of IE. It appears that Netscape handles and reports carriage returns in the <textarea> differently than IE, so the counters diverge. I was hoping that someone might be able to point me to a reference source or two where I can research the problem.

TIA for any assistance you can provide.

Best regards,
John
jbelmonte@prontomail.com

glenngv
01-31-2003, 11:19 AM
I have experienced that same thing in the past.
I found out that NS6 treats the line breaks entered by user in a textarea as '\n' only, while IE and NS4 treat it as '\r\n' (carriage return and linefeed).

here's the solution:


if (navigator.appName=="Netscape" && parseInt(navigator.appVersion)>=5)
desc=document.formNameHere.textareaNameHere.value.replace(/\n/g,'\r\n');
else desc=document.formNameHere.textareaNameHere.value;
alert(desc.length);