Forum Moderators: open
var a= " .... line 1.....\n"
a=a+"..... line 2 ....\n"
a=a+"..... line 3 ....\n"
this is tiresome
any easy way to do it? (don't want to read from a form)
===========================
var a="
line 1
line 2
line 3
"
this gets an script error.
===============================
<style type="text/css">
textarea { background: #F6F6F7; color: black;
border: 1px solid gray; clear: left; }
.label { font: 16px Verdana, Tahoma, Helvetica, Sans-Serif; }
.r { margin-left: 70px; }
.rlabel { font: 16px Verdana, Tahoma, Helvetica, Sans-Serif;
margin-left: 14px; margin-right: 5px;
float: left; }
</style>
<script type="text/javascript">
<!--
var strDone = null;
var strTmp = null;
function getVar(vname) {
var cleaned = document.form.input.value;
if (cleaned.length > 1) {
if (!vname) {
vname = prompt("Enter a variable name:", "");
}
var ret = "var " + vname + " = \"";
// escape escapes
cleaned = cleaned.replace(/\\/g, "\\\\");
// escape newlines
cleaned = cleaned.replace(/\n/g, "\\n");
// escape less-thans
cleaned = cleaned.replace(/\</g, "\\\<");
// escape greater-thans
cleaned = cleaned.replace(/\>/g, "\\\>");
// escape single quotes
cleaned = cleaned.replace(/\'/g, "\\\'");
// escape double quotes
cleaned = cleaned.replace(/\"/g, "\\\"");
// escape forward slashes
cleaned = cleaned.replace(/\//g, "\\\/");
ret += cleaned + "\";";
strTmp = cleaned;
document.form.output.value = ret;
document.form.output.select();
strDone = true;
}
else {
alert("Input string required.");
strTmp = null;
strDone = null;
}
}
function tstIt() {
if (strDone && strTmp) {
var locTmp = strTmp;
locTmp = locTmp.replace(/\\\\/g, "\\");
locTmp = locTmp.replace(/\\n/g, "\n");
locTmp = locTmp.replace(/\\\</g, "<");
locTmp = locTmp.replace(/\\\>/g, ">");
locTmp = locTmp.replace(/\\\'/g, "\'");
locTmp = locTmp.replace(/\\\"/g, "\"");
locTmp = locTmp.replace(/\\\//g, "\/");
document.open();
document.writeln("<pre>");
document.writeln(locTmp);
document.writeln("</pre>");
document.close();
}
else {
alert("Convert a string first with the To Variable button.");
}
}
//-->
</script>
...
<div>
<form name="form">
<span class="rlabel">Input: </span>
<textarea name="input" rows="15" cols="90"></textarea>
<br />
<span class="label">Output: </span>
<textarea name="output" rows="15" cols="90"></textarea>
<br />
<input class="r" type="button" value="To Var" onclick="getVar();"/>
<input type="button" value="Test it" onclick="tstIt();"/>
</form>
</div>
Jordan
Ps. Tested it using a 1703 line string, had no problems.
Pss. For all you JS heads wondering why not just use escape() instead of all those replaces; three reasons --
- 1. Alot of text editors try to wrap 'nicely' on virtual boundries like spaces so as not to split words, but escape() encodes those as well, meaning the ouput won't wrap correctly on them.
- 2. Look at the length of the string returned from this function for escape() vs. how I have it with replace(), using arbitrary Perl data as input;
Original string: 969 chars, 114 words, 26 lines
escape()'d string: 1972 chars, 495 words, 1 line
replace()'d String: 1086 chars, 143 words, 1 line
...it's slightly more code to use replace(), but the end result is better economy of ouput because we only need a few characters escaped, we don't need half of them URI-encoded.
- 3. Using escape() would require unescape()'ing the ouput variable at the time of use; using replace() with standard escapes ("\") doesn't. The output variable can be treated as a regular string this way.