Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Keeping whitespace in responseText, etc.

Reply
Thread Tools

Keeping whitespace in responseText, etc.

 
 
e271828
Guest
Posts: n/a
 
      08-24-2006
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 document.documentElement.innerHTML returns the
HTML source, but adds HEAD and other elements if they are absent from
the source, and takes out whitespace (i.e., line feeds, carriage
returns and tabs) within tags and between tags. The follow function:

function xhr() {

xhr = new XMLHttpRequest()
xhr.open("GET","test-page.html",true);
xhr.onreadystatechange = function() {
if (xhr.readyState==4) {
alert(xhr.responseText);
}
}
xhr.send(null)
}

doesn't add or alter any tags that are absent in the source, and does
not take out line feeds within tags; it does, however, still take out
all non-line-feed whitespace within tags and all whitespace in general
between tags.

It seems that preserving whitespace is all that I need, but I haven't
found a way to do that through my searches. So is there any way to get
the unaltered HTML source of a page without innerHTML or applets, like
a better version of the XMLHttpRequest object's responseText method?

Thanks,
Eric

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-24-2006


e271828 wrote:


> alert(xhr.responseText);



> doesn't add or alter any tags that are absent in the source, and does
> not take out line feeds within tags; it does, however, still take out
> all non-line-feed whitespace within tags and all whitespace in general
> between tags.


responseText gives you the text as the browser decodes it from the HTTP
response body. There might be issues with responseText with properly
decoding characters depending on the encoding of the response but I
don't think that the white space stripping occurs that you claim above.

I suspect rather that you use Mozilla respectively Firefox and that the
white space issue you notice is simply the somehow broken alert dialog
in Mozilla where lots of white space is collapsed and not rendered.
For example if you do e.g.
alert(['Line 1', ' Line 2', 'Line 3'].join('\r\n'))
with Mozilla then the alert dialog will not show the white space at the
beginning of Line 2 at all.

Is that Mozilla you are using? Then I think the issue you see is simply
alerting the responseText and not white space missing in responseText.

Or which browser do you have where you think that white space gets lost
when using responseText?

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
e271828
Guest
Posts: n/a
 
      08-24-2006
You were right, more or less. Unlike innerHTML, responseText doesn't
alter the HTML it gets; but when shown in an alert box it can seem like
responseText mangles whitespace . When you try
responseText.split("\t") or .split("\n") in a for loop for as many
results those methods return, however, you will see that the number of
the last alert plus 1 equals however many tabs or new lines you have in
your actual source (unlike innerHTML).


--


But now I've encountered another problem I haven't been able to find an
answer to: how do I get external URL's (say, http://www.google.com) to
open in the XMLHttpRequest, instead of just local files?


Martin Honnen wrote:
> e271828 wrote:
>
>
> > alert(xhr.responseText);

>
>
> > doesn't add or alter any tags that are absent in the source, and does
> > not take out line feeds within tags; it does, however, still take out
> > all non-line-feed whitespace within tags and all whitespace in general
> > between tags.

>
> responseText gives you the text as the browser decodes it from the HTTP
> response body. There might be issues with responseText with properly
> decoding characters depending on the encoding of the response but I
> don't think that the white space stripping occurs that you claim above.
>
> I suspect rather that you use Mozilla respectively Firefox and that the
> white space issue you notice is simply the somehow broken alert dialog
> in Mozilla where lots of white space is collapsed and not rendered.
> For example if you do e.g.
> alert(['Line 1', ' Line 2', 'Line 3'].join('\r\n'))
> with Mozilla then the alert dialog will not show the white space at the
> beginning of Line 2 at all.
>
> Is that Mozilla you are using? Then I think the issue you see is simply
> alerting the responseText and not white space missing in responseText.
>
> Or which browser do you have where you think that white space gets lost
> when using responseText?
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      08-25-2006


e271828 wrote:


> But now I've encountered another problem I haven't been able to find an
> answer to: how do I get external URL's (say, http://www.google.com) to
> open in the XMLHttpRequest, instead of just local files?


Inside the normal browser context the same origin policy applies for
request XMLHttpRequest makes thus you can only successfully make
requests back to the server your document with the script comes from.
So with client-side script you would need to have your own server side
script function as a proxy to fetch URLs from e.g. www.google.com.

Outside of the browser sandbox (e.g. on Windows in a HTA (HTML
application) or with a Windows Script Host script or an ASP page or with
the Mozilla browser if you write an extension) those restrictions do not
apply.

IE also has a zone model with different security zones which can be
configured separatedly where for the trusted zone for instance you could
change the settings to allow the request to other domains.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM
Div control and additional whitespace Donal McWeeney ASP .Net 2 10-20-2003 02:17 PM
Remove Whitespace when rendering Server Control threecrans ASP .Net 0 07-03-2003 03:25 PM



Advertisments