Javascript: newline in string shows different string length depending on browser

Written on 18 March 2011, 11:08am

Tagged with: , ,

Try the following example in your browser.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test js length</title>
#foo {width: 420px; height:70px; font-size:12px;}
#sbmt_btn {font-size:12px;}


<cfset str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Pellentesque bibendum tristique dignissim.">

String Length=#len(str)# characters

<form action="##" name="form1" id="form1" method="post" onsubmit="myfunc()">
	<textarea name="foo" id="foo" cols="30" rows="3">#str#</textarea> <br />
	<button type="submit" name="sbmt_btn" id="sbmt_btn">Submit</button>
<script type="text/javascript">
function myfunc()
	var myval = document.getElementById('foo').value;


Firefox will show 100 characters, while Internet Explorer 101. That’s because Firefox will count the newline as one character, while Internet Explorer – as two characters.

firefox - js alert showing 100 chars
internet explorer - js alert showing 101 chars

Tests performed in FF 3.6.15 and IE 8.0.60001.
Google Chrome has the same behavior as Firefox.

Leave a response