Forum Moderators: open

Message Too Old, No Replies

textarea length differences between IE/NE

         

samix

10:55 am on Oct 7, 2002 (gmt 0)

10+ Year Member


Hi everyone, I'm new here, and I'm not sure should this message be here, because it's more like javascript problem, but still it's that much browser problem too.

I have a following page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Samix textcounter</TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="javascript">
function countlen() {
var arealen = document.form1.area.value.length;
document.form1.counter.value = "length="+arealen;
}
</script>
<FORM id="form1" name=form1>
<textarea name="area" onkeyup="javascript:countlen();"></textarea><br>
<input type="text" name="counter" value="length=0">
</form>
</BODY>
</HTML>

Which works fine, except with netscape and mozilla, it gave me a wrong length! Every enter press increase that value by 2, with IE and Opera, but Mozilla and Netscape 6 increase that value by one!
So one enter length with IE/Opera is 2, but with Netscape/Mozilla counter says it's one, but reality is that, that their length after POST is 2 too!

So, is this some known bug, or what I missed, and how can I fix it? For my system, it's very important to get correct length in that counter with Netscape and Mozilla too.

Purple Martin

6:54 am on Oct 8, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've just tested it in IE6, N4, N7. It worked fine in all of them.

You're using Javascript to get the length property of a string, which is good - I can't really see how this would go wrong. Can you give more detail about how we can reproduce the error?

jatar_k

7:39 am on Oct 8, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I tested it in moz and it works perfectly. Neat little page.

samix

6:25 am on Oct 9, 2002 (gmt 0)

10+ Year Member



I didn't mean that it gives any "error". Three enter press just give length six to the counter box with IE, but three enter press are just three length when using Mozilla/Netscape 6.
Last count is incorrect. Real length is six.

So following text length with ie is 10, but with netscape length is 7

a
b
c
d

TheDave

6:45 am on Oct 9, 2002 (gmt 0)

10+ Year Member



It's probably got something to do with the carriage return combination used by explorer or netscape being different. chr(10) = line feed, chr(13) = carriage return (or vice-versa). I'm guessing one browser uses just a chr(10) while the other uses a chr(10) & chr(13) combination. You'll need to remove these characters from the string, I'd use:

tmpStr = ..form1.area..
c=0
l=len(tmpStr)
outStr = ""
while c < l
c=c+1
midStr = mid(tmpStr, c, 1)
if not midStr = chr(10) and not midStr = chr(13) then outStr = outStr & midStr
wend
arealen = len(outStr)

err.. that's vb (i grabed it from a server script) I just realised your using java, but it should be easy to convert

Purple Martin

9:14 am on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



TheDave is right - different browsers are using different character combinations for line feeds/returns. And yes, you can use JavaScript to remove/replace characters as needed.

An extra bit of info: different operating systems also use different character combinations for line feeds/returns. For example, Mac OS and Windows do it differently.