views:

107

answers:

3

OK I don't use js enough to know, but is there a way to get the real source code of the page with it?

document.body.innerHTML for example gives some kind of "fixed up" version where malformed tags have been removed.

I'm guessing using XMLHttpRequest on the original page might work, but seems kind of stupid.

+1  A: 

I typically use FireBug when I want to peruse or copy source files.

Upper Stage
+3  A: 

For accessing JS of the same origin, XMLHttpRequest is quite fine. You can have access to any JS document in "raw" format using this technique without the browser getting in the way (i.e. conversion to DOM and back).

I am not sure I understand your comment re: XMLHttpRequest being stupid : is it because you are worried about the potential duplication of work? i.e. getting the code 2times from the origin server.

jldupont
+4  A: 

This happens because browsers parse the DOM and don't keep the HTML in memory. What is returned to you is the browser's conversion of the current DOM back to HTML, which is the reason for the uppercase tags and lack of self closing tags where applicable.

An XMLHttpRequest would be the best way to go. In most cases, assuming the server doesn't send the no-cache header, and the HTML page has finished downloading, the XMLHttpRequest would be almost instant because the file is fetched from the cache.

Andy E
However, if the page is dynamically generated on the server, the content may have changed between the original load and getting it via an XMLHttpRequest.
ntownsend
Thanks all for the extremely fast advice! I need malformed html/javascript to parse itself and I guess it doesn't make it into the DOM though it gets executed. Yes, I suppose XMLHttpRequest will be fast enough.
graw
@ntownsend - a very good point.
Andy E