views:

47

answers:

3

I need to get the entire contents of a page with javascript and send it to a server script to save it. I want to do this after the user has made some changes to the page using AJAX and other javascript tools. I don't want the state of certain elements. I'd like to essentially get everything inside the body tag so I can pass it to a server-side script. I have tried getelementbyid etc. but it seems to put the page in a loop and crashes.

Thanks

+1  A: 

Sure you can, if you want to. There's an element of the DOM tree called 'document' containing the whole document. There are ways to translate a DOM tree to HTML in most libraries; applying that to the document node should do the job.

Some hacking about long those lines should get you on the right track.

But do you really need to do that? Why?

Charlie Martin
Well..it's kinda crazy..but it occured to me that i could make an extremely lazy CMS for a static site. I'll just put ckeditor on each page when the user is done making changes I'll save the entire page back as a static html page.versioning should be easy to, just add some basic login and done.
Dogbot
Not crazy at all, that's basically how a wiki works. Should be fun to see; let me know.
Charlie Martin
+1  A: 

Try the following code:

var body = document.getElementsByTagName("body");
var bodycontent = body[0];

Then use "bodycontent.innerHTML" to retrieve the contents of it. If I'm not mistaken, it should provide the body's current content, after any javascript modifications that have been made to it.

Doktor J
alternatively, "body[0].innerHTML" would work, if you wanted to eliminate the "bodycontent" variable, or if you wanted to be very terse, you could simply use "document.getElementsByTagName("body")[0].innerHTML" to access the contents without using any variables -- it works, but is somewhat less easy to read ;)
Doktor J
fantastic I will try these today!
Dogbot
A: 

It should work simply like this:

javascript: document.body.innerHTML;

jquery: $("body").innerHTML

netadictos
cool, I am alreading using jquery on my pages. thanks
Dogbot