473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XHR responseText containing literal newlines (\n).

For example:

var newlines = 'a\n\nb\n\nc';
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

....not...
a
b
c

Anyone know offhand if it is possible to get the alert to use the \n's
found in a responseText?

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 4 '07 #1
4 5069
On May 4, 8:01 am, -Lost <maventheextrawo...@techie.comwrote:
For example:

var newlines = 'a\n\nb\n\nc';
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

...not...
a
b
c

Anyone know offhand if it is possible to get the alert to use the \n's
found in a responseText?
I'm not clearly understand what exactly you want.
Which one do you need?

function ex(xhrObj)
{
if (/\\n/mg.test(xhrObj.responseText))
alert('newline');

alert(xhrObj.responseText.replace(/\\n/mg,"\n"));
}

HTH
May 4 '07 #2
Cah Sableng wrote:
On May 4, 8:01 am, -Lost <maventheextrawo...@techie.comwrote:
>For example:

var newlines = 'a\n\nb\n\nc';
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc

...not...
a
b
c

Anyone know offhand if it is possible to get the alert to use the \n's
found in a responseText?

I'm not clearly understand what exactly you want.
Which one do you need?

function ex(xhrObj)
{
if (/\\n/mg.test(xhrObj.responseText))
alert('newline');

alert(xhrObj.responseText.replace(/\\n/mg,"\n"));
}
Thanks, that works. (The replacement not the test.)

Maybe I am missing something fundamental, but replacing \n with \n seems
really, really funny to me.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 4 '07 #3
rf

"-Lost" <ma****************@techie.comwrote in message
news:JP******************************@comcast.com. ..
For example:

var newlines = 'a\n\nb\n\nc';
'a\n\n\b\n\n\c' above contains the letter 'a' then the character '\' then
the letter 'n' then '\' then 'n' then 'b'

The value of the variable newlines above is, after parsing of 'a\n\nb\n\nc'
by the interpreter and assigning the result to the variable, the letter 'a'
then a newline then newline then the letter 'b' then...
alert(newlines);

Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:

a\n\nb\n\nc
It's not the "exact same line".

What you get in the response text is the string 'a\n\n\b\n\nc'.

What you get up the top there is this string parsed by the javascript
interpreter who carefully translates the character sequence \n to a newline
character.

BTW xhrObj.responseText.replace(/\\n/mg,"\n"); is not translating \n into
\n, or even a newline into a newline. It is searching for the two character
string, \n, with the \ escaped of course, so we write it /\\n/. It is
replacing occurrences of this string with something, the something being the
string "\n" which has of course been parsed by the interpreter into a string
containing the single character 'newline'.

--
Richard.


May 4 '07 #4
On May 4, 11:31 am, "rf" <r...@invalid.comwrote:
"-Lost" <maventheextrawo...@techie.comwrote in message

news:JP******************************@comcast.com. ..
For example:
var newlines = 'a\n\nb\n\nc';

'a\n\n\b\n\n\c' above contains the letter 'a' then the character '\' then
the letter 'n' then '\' then 'n' then 'b'

The value of the variable newlines above is, after parsing of 'a\n\nb\n\nc'
by the interpreter and assigning the result to the variable, the letter 'a'
then a newline then newline then the letter 'b' then...
alert(newlines);
Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:
a\n\nb\n\nc

It's not the "exact same line".

What you get in the response text is the string 'a\n\n\b\n\nc'.

What you get up the top there is this string parsed by the javascript
interpreter who carefully translates the character sequence \n to a newline
character.

BTW xhrObj.responseText.replace(/\\n/mg,"\n"); is not translating \n into
\n, or even a newline into a newline. It is searching for the two character
string, \n, with the \ escaped of course, so we write it /\\n/. It is
replacing occurrences of this string with something, the something being the
string "\n" which has of course been parsed by the interpreter into a string
containing the single character 'newline'.
Umm..
I think I saw string variable enclosed inside single quotes before
replied.. :)
But your last paragraph is right.

May 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike | last post by:
Hi. I am trying to read a simle text file into a string variable, alter the text using string.Replace(), then use the resulting text as the body for an email. This works fine up until the...
0
by: Pat Sagaser via .NET 247 | last post by:
I'm trying to add LinkButtons to a Repeater control using adynamic template. The docs state that you should be able tobubble the click event to the containing Repeater. There areplenty of examples in...
1
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with...
3
by: e271828 | last post by:
I'm trying to access the source of an HTML page with as few alterations from the actual source (as in, that seen from the View Source option) as I can. The method...
9
by: will | last post by:
I have an XML input that includes things like: <foo>line of text another line of text yet another</foo> And I want the entities PRESERVED (not translated) on the result, so: <bar>line of...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
1
by: abrtlt | last post by:
I have a simple PHP script that sends a string to XMLHttpRequestObject.responseText. responseText as received from the PHP script begins with 2 extra newline characters (\n\nMyString): <?php...
5
by: marcus6 | last post by:
Howto access xmlHttp.responseText as document.getElementByTagName ? If I access a .html page through the xmlHttp, how can I use the functions .getElementsByTagName and .getElementById on the data...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.