Multi Line Strings: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m minor coding style ( better Readability and maintainability especially regarding the first & last line) |
|||
Line 7: | Line 7: | ||
<pre class='sample'> | <pre class='sample'> | ||
var longString = "Lorem ipsum dolor sit amet, " + | var longString = | ||
"Lorem ipsum dolor sit amet, " + | |||
"venenatis penatibus etiam. " + | "venenatis penatibus etiam. " + | ||
"Nec purus cras elit nec. " + | "Nec purus cras elit nec. " + | ||
"Elit pharetra hymenaeos. " + | "Elit pharetra hymenaeos. " + | ||
"Donec at cubilia pulvinar elit. " + | "Donec at cubilia pulvinar elit. " + | ||
"Aliquet pretium tortor montes maecenas ante amet vel bibendum."; | "Aliquet pretium tortor montes maecenas ante amet vel bibendum." + | ||
""; | |||
</pre> | </pre> | ||
Line 24: | Line 26: | ||
<pre class='sample'> | <pre class='sample'> | ||
var longString = ["Lorem ipsum dolor sit amet, ", | var longString = [ | ||
"Lorem ipsum dolor sit amet, ", | |||
"venenatis penatibus etiam. ", | "venenatis penatibus etiam. ", | ||
"Nec purus cras elit nec. ", | "Nec purus cras elit nec. ", | ||
Line 38: | Line 41: | ||
<pre class='sample'> | <pre class='sample'> | ||
var longString = "Lorem ipsum dolor sit amet, \ | var longString = "\ | ||
Lorem ipsum dolor sit amet, \ | |||
venenatis penatibus etiam. \ | venenatis penatibus etiam. \ | ||
Nec purus cras elit nec. \ | Nec purus cras elit nec. \ | ||
Elit pharetra hymenaeos. \ | Elit pharetra hymenaeos. \ | ||
Donec at cubilia pulvinar elit. \ | Donec at cubilia pulvinar elit. \ | ||
Aliquet pretium tortor montes maecenas ante amet vel bibendum."; | Aliquet pretium tortor montes maecenas ante amet vel bibendum.\ | ||
"; | |||
</pre> | </pre> | ||
Revision as of 02:42, 16 April 2013
Sometimes, it is desirable to use a multi line string in JavaScript, such as when adding css styles. Here are some approaches, and discussion about the strengths and weaknesses of each approach.
Concatenation
The basic method:
var longString = "Lorem ipsum dolor sit amet, " + "venenatis penatibus etiam. " + "Nec purus cras elit nec. " + "Elit pharetra hymenaeos. " + "Donec at cubilia pulvinar elit. " + "Aliquet pretium tortor montes maecenas ante amet vel bibendum." + "";
Pros:
- Simple to understand.
Cons:
- Extra syntax at the head and tail of every line.
- JavaScript string concatenation has poor performance characteristics.
A very similar but more efficient approach would define an array of many strings, then join them into one long string:
var longString = [ "Lorem ipsum dolor sit amet, ", "venenatis penatibus etiam. ", "Nec purus cras elit nec. ", "Elit pharetra hymenaeos. ", "Donec at cubilia pulvinar elit. ", "Aliquet pretium tortor montes maecenas ante amet vel bibendum." ].join("");
Line continuation
JavaScript can continue lines, via trailing backslashes, like C:
var longString = "\ Lorem ipsum dolor sit amet, \ venenatis penatibus etiam. \ Nec purus cras elit nec. \ Elit pharetra hymenaeos. \ Donec at cubilia pulvinar elit. \ Aliquet pretium tortor montes maecenas ante amet vel bibendum.\ ";
Pros:
- Efficient.
Cons:
- An uncommon technique, therefore not as well understood.
- Requires extra syntax, albeit not as much as with concatenation.
Using @resource
If the string is too long or too big — e.g. CSS code, JSON data — you can use @resource metadata in your script and get its content using GM_getResourceText API.
First, add @resource
in metadata block
// @resource resourceName http://www.example.com/example.ext
You can then do something like
alert(GM_getResourceText("resourceName"));
GM_addStyle(GM_getResourceText("resourceName")); // if resource content is CSS
element.innerHTML = GM_getResourceText("resourceName"); // if resource content is HTML
var data = JSON.parse(GM_getResourceText("resourceName")); // if resource content is JSON
or whatever.
Pros:
- Editor syntax highlighting will work.
Cons:
- Requires file to be downloaded at install time.
- Low cross-platform compatibility.