PDA

View Full Version : Reading current page's HTML???



x_goose_x
Mar 30th, 2003, 06:43 AM
The only thing I found that worked was:

document.body.parentElement.outerHTML

is there a more direct way? I need it to be used within my script (can't use view-source:)

joh6nn
Mar 30th, 2003, 08:24 AM
in gecko, there's document.documentElement.inner/outerHTML . don't know if IE supports that, though

liorean
Mar 30th, 2003, 01:19 PM
This is a problem point - there's no standardised way of doing it and the IE way doesn't really work that well. I suggest you use something like this:


function getHTML(element){
var
scope=element||document,
i,
j=0,
aSource=[],
sSource='';
switch((i=top.XMLSerialiser||scope.all||scope.outerHTML)){
case XMLSerializer:
sSource=(new i).serializeToString(scope);
break;
case scope.all:
do aSource[aSource.length]=i[j].outerHTML;
while(i[j++].nodeType!=1);
sSource=aSource.join('\n');
break;
default:
sSource=i;
// This depends on outerHTML being existant.
// You could do this through childNodes,
// but I really didn't feel up to writing
// the needed script.
}
return sSource;
}

then you simple call it with getHTML(); If you want the source of only a part of the html document you can use getHTML(element);

x_goose_x
Mar 30th, 2003, 04:04 PM
Originally posted by joh6nn
in gecko, there's document.documentElement.inner/outerHTML . don't know if IE supports that, though

Oddly enough the innerHTML part works in my Moz1.3b, but the outerHTML part doesn't. I suppose I could always just add the <html> tags manually.

Thanks for the script liorean, however it looks like it's more work than it's worth. I think I'll just use something like:



if ( navigator.appName == "Microsoft Internet Explorer" ) {
alert(document.body.parentElement.outerHTML);
} else {
alert("\<html\>"+document.documentElement.innerHTML+"\<\/html\>");
}