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>
Just put a \
at the end of each line still in the string
str = " The quick brown \ // <---
fox jumped over the log.";
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With