View Full Version : Grabbing page source with Javascript

03-04-2005, 10:20 AM
Ok. I've been looking all night and can't find the answer. This may be impossible but I don't think it is:

I want to use a javascript to pass the page source of an HTML document to a CGI script.

All I need to find out is how do I assign the current page's source to a variable in javascript?

I mean I know I can do something like:

var t = document.title;

And that gets me the title of the page. But how do I just get a dump of the document source?

Is there a way?

Any help would be very much appreciated!

03-04-2005, 10:32 AM
document.documentElement.innerHTML will get you most of it, or IE has a "download behavior" which can get it 'as authored', or there's possibly a way to use a request method of an xml object (not sure...).

03-06-2005, 09:38 PM
Hrm...well I definately need all of the source. This sort of works but doesn't give me complete info....

I would prefer to stay away from anything browser specific as I don't want to have to force the user into a browser....

So what's the scoop with the XML stuff? From what I can find its something like: XMLHTTPRequest but that, from what I can tell, creates another request to the server. Can someone expand a little on this for me? An example script would be perfect as I learn by example... :)

03-07-2005, 03:12 AM
If you need the current state of the source (if the DOM has been altered), then innerHTML might be the only way--short of inserting diffs into the original.
The request object does its thing in-process, I think; there are several examples of its usage archived in these forums, btw.