开发者

Why can't I store multi line values into a JavaScript literal? [duplicate]

开发者 https://www.devze.com 2023-03-08 16:03 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicates: a more graceful multi-line javascript string me开发者_如何学Pythonthod
This question already has answers here: Closed 11 years ago.

Possible Duplicates:

a more graceful multi-line javascript string me开发者_如何学Pythonthod

Multiline strings in Javascript

window.lastSavedContents = "test

tester";

That's my JavaScript Code. I get a firebug error saying:

unterminated string literal [Break On This Error] window.lastSavedContents = "test


Indeed; that's simply not valid syntax. Use "\n" to represent a newline:

window.lastSavedContents = "test\n\ntester";


If you want to mark new line in a JavaScript value you have t put there a newline character by putting \n:

var test = "test\n\ntest";

You can also visually expose new lines by escaping ends of lines like this:

var test = "test\n\
\n\
test";

http://jsfiddle.net/grcLH/1/


You can concatenate it, of course:

var stringTxt = "";

stringTxt += "test";
stringTxt += "tester";

window.lastSavedContents = stringTxt;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号