Javascript: newline in string shows different string length depending on browser
Written on 18 March 2011, 11:08am
Tagged with: browsers, coldfusion, javascript
Try the following example in your browser.
simple_jstest.cfm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test js length</title>
<style>
#foo {width: 420px; height:70px; font-size:12px;}
#sbmt_btn {font-size:12px;}
</style>
</head>
<body>
<cfset str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque bibendum tristique dignissim.">
<cfoutput>
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>
</form>
</cfoutput>
<script type="text/javascript">
function myfunc()
{
var myval = document.getElementById('foo').value;
alert(myval.length);
}
</script>
</body>
</html>
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.
Tests performed in FF 3.6.15 and IE 8.0.60001.
Google Chrome has the same behavior as Firefox.
Written by Dorin Moise (Published articles: 277)
- Likes (0)
-
Share
- Comments (0)