views:

83

answers:

2

I need a java script function that converts the document object of the current loaded page back to it's source text. In firefox it's smth like that:

var doc = document;
var str = (new XMLSerializer()).serializeToString(doc);
alert(str);

But i need a cross browser solution. How would this be done?

For example:

<html>
  <body>
    <script>
       alert( asText(document) );
    </script>
  </body>
</html>

would pop up:

<html>
  <body> 
    <script>
       alert( asText(document) );
    </script>
</html>

how would you implement the 'asText' function?

+7  A: 

Why do not you use document.documentElement.innerHTML?

example

Eldar Djafarov
bcs it seem's yesterdays night was too long ... thanks!
Chris
A: 
function sourceText(){     
 try{
  var O= new XMLHttpRequest();
  O.open('GET', location.pathname, false);
  O.send(null);
  return O.responseText;
 }
 catch(er){
  return '';
 }
}
kennebec
nice idea, what if the current site was obtained with a POST and got passed some parameters?
Chris