admin管理员组文章数量:1405393
From Test.html I include a test.js that runs function d(). This function has a parameter a string that might contain "new line" characters (10 in ASCII), as per example below. However, when I am debugging this function, vars s and data include all characters except the new line, i.e., they contain string "HelloWorld" (length 10) instead of "Hello[charNewLine]World" (length 11).
Is there a way to pass a string with new line characters as parameter to a function in Javascript? Thank you.
Test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=x-user-defined">
<title>JavaScript Scripting</title>
</head>
<body>
<script type="text/javascript" charset="x-user-defined" src="test.js">
</script>
</body>
</html>
test.js
function d(s)
{
var data = (s + "").split("");
var dataLength = data.length;
var t = [dataLength],n;
for(n=0;n<dataLength;n++)
t[n]=data[n].charCodeAt(0);
}
d("Hello\
World");
From Test.html I include a test.js that runs function d(). This function has a parameter a string that might contain "new line" characters (10 in ASCII), as per example below. However, when I am debugging this function, vars s and data include all characters except the new line, i.e., they contain string "HelloWorld" (length 10) instead of "Hello[charNewLine]World" (length 11).
Is there a way to pass a string with new line characters as parameter to a function in Javascript? Thank you.
Test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=x-user-defined">
<title>JavaScript Scripting</title>
</head>
<body>
<script type="text/javascript" charset="x-user-defined" src="test.js">
</script>
</body>
</html>
test.js
function d(s)
{
var data = (s + "").split("");
var dataLength = data.length;
var t = [dataLength],n;
for(n=0;n<dataLength;n++)
t[n]=data[n].charCodeAt(0);
}
d("Hello\
World");
Share
Improve this question
asked Feb 25, 2012 at 23:46
user411103user411103
2 Answers
Reset to default 3d("Hello\
World");
contains a "line continuation".
The language specification says
The String value (SV) of the literal is described in terms of character values (CV) contributed by the various parts of the string literal.
...
The SV of
LineContinuation :: \ LineTerminatorSequence
is the empty character sequence.
which means that it contributes no characters to the value of the string literal.
To embed a newline in a string, just use \n
as in
d("Hello\nWorld")
If you want to see the new line and also pass it in the string, use it this way:
d("Hello\n\
World");
本文标签: New line character as function parameter in JavascriptStack Overflow
版权声明:本文标题:New line character as function parameter in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744284764a2598823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论