Using E4X, Firefox supports true multi-line strings in JavaScript; no need for concatenating strings or escaping end-of-lines, just wrap everything in an <r> and CDATA tags:
var the_old_way_1 = "this is a multi-line \ string.\n\ This is its second line."; /* this is a multi-line string This is its second line. */ var the_old_way_2 = "this is also a multi-line " + "string.\n" + "This is its second line."; /* this is also a multi-line string. This is its second line. */ var the_E4X_way = "" + (<r><![CDATA[ this is a cleaner multi-line string. This is its second line. I can have whatever quotes and tags I like in it, <foo></foo> "hello, I'm quoted." // this is not a comment! ]]></r>); // this IS a comment. /* this is a cleaner multi-line string. This is its second line. I can have whatever quotes and tags I like in it, <foo></foo> "hello, I'm quoted." // this is not a comment! */
Note that it important to concatenate this with a string because the E4X is not of the string type by default (and the concatenation will cast it into a string). Another way to accomplish this is to use toString(), like this: var the_E4X_way2 = (<r><![CDATA[ foobar ]]></r>).toString(); You'll also notice that the extra carriage returns between the CDATA's inner-most square brackets are interpreted literally, giving extra lines to your string. You can start your string right away if you like, I just find it easier to have the extra spacing.
Caveats: E4X does not work in Opera or IE, an even vim's syntax highlighting fails to understand what it is without the vim after-syntax for E4X script I wrote.
Example: My Userscripts.org - Face icons, tweaks has almost its entire contents encapsulated in an E4X string. Happily, the syntax highlighting for it isn't so bad (and in vim, I used nested comments to properly highlight everything as CSS).
login to vote
you might want to add that you can also just write straight up HTML without escaping anything as part of E4X.
login to vote
TAG SOUP ALERT! ;) What exactly is an r node?
login to vote
An "r node" isn't anything; it's just an arbitrary tag name. You actually don't need to use one at all — "
(<><![CDATA[Your text here.]]></>).toString()" will also work.login to vote
Cool, thanx for the info khopesh
login to vote
I used
<r>because it is a non-HTML tag and because some editors can match those tags. When my E4X includes CSS, I use<style>and when it is HTML, I use<html>. More importantly, it's more human-readable than the otherwise consecutive six non-alphanumeric/non-space characters. Angle brackets opening towards each other look like a diamond and are sometimes rendered without a space.login to vote
Unfortunately other browsers do not recognize the E4X syntax. However, at least in Opera, you can use:
var the_function_way = function(){/* this is a multi-line string This is its second line. */}.toString();Depending on what you need the string for, you may also need to strip off the first and last lines (the function enclosers).This doesn't work in Firefox because Firefox helpfully (?) pre-processes the function and strips out the comments.
login to vote
Very useful guide (& comments). I used this method in a script which replaced a huge block of HTML code on a page.
Removing the line breaks would make the code unreadable. Using the old method would be too time-consuming. And converting that HTML code (which I simply copied from another place) into JS/DOM (using "createElement"s, properties, "appendChild"s, etc.) would be a real torture, not to mention it would slow down the script.
login to vote
Now also available here:
http://wiki.greasespot.net/Multi_Line_Strings