...

View Full Version : How to GET the source code of the document?



DHTML Kitchen
01-02-2007, 11:43 PM
I want to validate generated html using w3c validation service.

I will need rendered source code as a string, in memory.

innerHTML - rendered tag soup (e.g. for text/html, "<br>" when source code is "<br/>").
view-source: + document.location - original (no generated HTML) source code

How to get rendered source code as a string, in memory?

Garrett

felgall
01-03-2007, 04:46 AM
var d = document.documentElement.innerHTML;
d=d.replace(/&/g,'&amp;');
d=d.replace(/</g,'&lt;');
d=d.replace(/>/g,'&gt;');

and now d can be output to where ever you want the source to display.

jkd
01-03-2007, 05:21 AM
felgall's solution excludes the actual <html> tag itself. In IE, you can just grab document.documentElement.outerHTML, while in Firefox/Opera, call:


(new XMLSerializer()).serializeToString(document)


That even grabs DOCTYPE and everything.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum