Forum Moderators: open

Message Too Old, No Replies

500 lines of text in a script var

easy way to do it?

         

affter333

11:38 pm on Sep 15, 2003 (gmt 0)



Hi, I need to put an 500 lines artical in a var

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.
===============================

Purple Martin

1:10 am on Sep 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Paste the article into MS Word. Replace all paragraph chars ^p with \n.

Then add var a = " to the front and " to the end.

Done in 5 seconds.

MonkeeSage

7:44 am on Sep 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a form I made for that purpose...you can use it to output a variable from a literal string for use in your scripts...

<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.