Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a long string with javascript [duplicate]

I have a simple problem that I just can't seem to figure out. In the code below I get an error (test_str is not defined) because the line defining "var str=" is spread across two lines. After the word "fox" there is a CR LF and I guess my javascript engine in my browser thinks I want a new statement there. Now of course in this example I can simply get rid of the carriage return and put it all on one line to fix it. But my real intent is for a much longer string in some production code which I don't really want to mess with deleting all those CR LF's.

<html>
<head>
<script>
function test_str() {
  str = "  The quick brown 
  fox jumped over the log.";
  alert(str);
}
</script>
</head>
<body>
    <a href='javascript:void(0);' onclick='test_str();'>Test String</a>
</body>
</html>
like image 739
Thread7 Avatar asked Dec 30 '13 18:12

Thread7


2 Answers

Just put a \ at the end of each line still in the string

str = "  The quick brown \  // <---
fox jumped over the log.";
like image 153
Deryck Avatar answered Sep 19 '22 13:09

Deryck


Easiest thing to do is to add a \ at the end of the lines:

function test_str() {
  str = "  The quick brown \
  fox jumped over the log.";
  alert(str);
}

jsFiddle example

like image 29
j08691 Avatar answered Sep 22 '22 13:09

j08691