PDA

View Full Version : Escape/Unescape and hard returns...


dremmers
01-09-2003, 03:06 AM
Is it true that escape replaces hard returns with %0D and unescaping should put them back in?

I'm having trouble with mine, but before I go into details, I want to make sure I have the idea right...

Thanks.

glenngv
01-09-2003, 03:22 AM
%0A for new line (\n)
%0D for carriage return (\r)

try this simple test:

<script>
var str = "line1\nline2"
var str2 = "line1\rline2"
alert("str:\n"+str+"\n\nEscaped:\n"+escape(str) + "\n\nstr2:\n"+str2+"\n\nEscaped:\n"+escape(str2))
</script>

dremmers
01-09-2003, 03:40 AM
Hmmm....

My problem is in a textarea on a form.

If the user types hard returns to separate paragraphs, I need to be able to show them on the redirect.

Sending out I have:

function storyResult(form)
{
form.redirect.value = 'http://www.mylifestory.org/MyStory.html' + '?' +
escape('Written by:') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getText(form.Story) + '&';


etc.

and

function getText(object) {
return escape(object.value)
}

And coming back I have:

var input = '', output = '';

if (location.search.length > 0)
input = location.search.substring(1);

while (input.length > 0) {
variableName = input.substring(0,input.indexOf('='));
input = input.substring(input.indexOf('=')+1);
variableValue = input.substring(0,input.indexOf('&'));
input = input.substring(input.indexOf('&')+1);

output += '<P><B>' + unescape(variableName) + '<\/B>';
output += '<p>' + unescape(variableValue) + '<\/P>';
}

document.write(output);

The carriage returns (%OD) appear in the URL, but for some reason they're not translating on the page? (It's as if they didn't exist, or were spaces...)

Do you see anything that I'm doing wrong?

glenngv
01-09-2003, 03:44 AM
since you are writing the output as html text not as textarea value, you need to change the line breaks to <br> or surround it with <pre></pre> tags to preserve the line breaks

dremmers
01-09-2003, 04:12 AM
Originally posted by glenngv
surround it with <pre></pre> tags to preserve the line breaks

I tried this first, but then I have MAJOR problems with text wrapping (I won't bore you with the long thread elsewhere on this forum!)

Originally posted by glenngv
since you are writing the output as html text not as textarea value, you need to change the line breaks to <br>

I'm confused about this suggestion. Are you saying to replace the <p> tags in:

output += '<P><B>' + unescape(variableName) + '<\/B>';
output += '<p>' + unescape(variableValue) + '<\/P>';

with <br>?

I tried this, but it only affected the textarea itself, not the paragraphs within the textarea value.:confused:

glenngv
01-09-2003, 05:20 AM
i said replace the line breaks (carriage returns not the <p> tags) with <br>

dremmers
01-09-2003, 01:49 PM
Excuse my ignorance, but how exactly do I do this?

I tried typing them manually in the textbox, and it works like a charm, but of course I can't ask my users to do this.

Is there something I can put in getText to do the conversion?


function getText(object) {
return escape(object.value)
}

dremmers
01-11-2003, 01:26 AM
OK, after some research, I realize I wasn't asking a simple question. I have added a function that escapes the text and then replaces the returns with <br>. This works great...to an extent.

I have a little checkbox that determines whether the info is sent via a perl script or not. My problem is that if it is NOT sent, everything works beautifully. If it IS sent, for some reason the text seems to be escaping TWICE. That is, the <br> is converted to &lt;br&gt; !!!!!!!

For the life of me I can't figure out where or why this is happening! :(

Here are the three functions that are called. The entire page can be viewed at http://www.mylifestory.org/questions/1.html

function storyResult(form)
{
form.redirect.value = 'http://www.mylifestory.org/MyStory.html' + '?' +
escape('Written by:') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getStory(form.Story,'<br>') + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
else
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******"))
{
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
else
{
window.open ("http://www.mylifestory.org/ebookSubmission.html","ebookSubmission","menubar=no,toolbar=no,location=no,scrollbars=no,resizable=no,status=no,left=200,top=150,height=300,w idth=250");
return true;
}
return true;
}
return true;
}

function getText(object) {
return escape(object.value)
}

function getStory(textarea,replaceWith) {
var StoryText = escape(textarea.value)
for(i=0; i<textarea.value.length; i++){

if(StoryText.indexOf("%0D%0A") > -1){
StoryText=StoryText.replace("%0D%0A",replaceWith)
}
else if(StoryText.indexOf("%0A") > -1){
StoryText=StoryText.replace("%0A",replaceWith)
}
else if(StoryText.indexOf("%0D") > -1){
StoryText=StoryText.replace("%0D",replaceWith)
}
}
return StoryText;
}

tempest1
01-11-2003, 04:12 AM
Not to add confusion but you also might want to make all the spaces &nbsp; to allow more flexibility there.

» ð, Ŵħąŧ¿ Ýőų łįķę¿

dremmers
01-11-2003, 12:00 PM
The problem was in my Perl Script!!!!!!!!

There was a line that converted < and > .

AAARRRGGGHHHH!!!!

All better now....thanks.