I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?
|
|||||
|
Javascript doesn't have a here-document syntax. You can escape the literal newline, however, which comes close:
|
|||||||||||||||||||||
|
the pattern To keep oversight with complex or long multiline strings I sometimes use an array pattern:
or the pattern anonymous already showed (escape newline), which can be an ugly block in your code:
|
|||||||||||||||||||||
|
Google's javascript style guide recommends to use string concatenation instead of escaping newlines because the latter isn't a part of ECMAscript:
I think you can try this and other features by first downloading canary chrome and then turning on |
|||||||||||||
|
You can have multiline strings in pure JavaScript. This method is based on the serialization of functions, which is defined to be implementation-dependent. It does work in the most browsers (see below), but there's no guarantee that it will still work in the future, so do not rely on it. Using the following function:
You can have here-documents like this:
The method has successfully been tested in the following browsers (not mentioned = not tested):
Be careful with your minifier, though. It tends to remove comments. For the YUI compressor, a comment starting with I think a real solution would be to use CoffeeScript. |
|||||||||||||||||||||
|
You can do this...
|
|||||||||||||||||||||
|
I'm surprised I didn't see this, because it works everywhere I've tested it and is very useful for e.g. templates:
Does anybody know of an environment where there is HTML but it doesn't work? |
|||||||||||||||||
|
I came up with this very jimmy rigged method of a multi lined string. Since converting a function into a string also returns any comments inside the function you can use the comments as your string using a multilined comment /**/. You just have to trim off the ends and you have your string.
|
|||||||||||||||
|
I solved this by outputting a div, making it hidden, and calling the div id by jQuery when i needed it. e.g.
Then when i need to get the string, i just use the following jQuery:
Which returns my text on multiple lines. If i call
I get: |
|||
|
I like this syntax and indendation:
(but actually can't be considered as multiline string) |
|||
|
Using script tags:
|
||||
|
to sum up, I have tried 2 approaches listed here in user javascript programming (Opera 11.01):
So I recommend the working approach for Opera user JS users. Unlike what the author was saying:
It DOES work in Opera 11. At least in user JS scripts. Too bad I can't comment on individual answers or upvote the answer, I'd do it immediately. If possible, someone with higher privileges please do it for me. |
|||||
|
Just tried the Anonymous answer and found there's a little trick here, it doesn't work if there's a space after backslash
But when space is removed it works -
Hope it helps !! |
|||||
|
I think this workaround should work in IE, Chrome, Firefox, Safari, Opera - Using jQuery :
Using Pure Javascript :
Cheers!! |
|||||||
|
This works in IE, Safari, Chrome and Firefox:
|
|||||||||||||
|
This works at least in Firefox:
|
|||||||
|
This seems to work in Fx 19 and Chrome 24 on Mac
|
|||
|
This is one fairly economical approach, at least in terms of the source code:
would be nicer of course if the "arguments" property were a proper array. A second version might use "" to do the join for cases when you want to control the line breaks in a very long string. |
||||
|
It's not extremely elegant but it's clean enough for me:
|
|||||||
|