views:

134

answers:

3

So is it possible to snag the entire content of a page in its current state. For example, if interacting with a page, via jquery, I've modified the document by either inserting content, or by adding or removing class names. Is it possible to get the markup of this document in its current form from starting html tag to html tag?

+4  A: 

Can't you just get the root object in the DOM and call innerHTML to get it all?

Lloyd
Yes we can! - think I heard this phrase somewhere else
Richard
Either Bob the Builder or Barack Obama
David Caunt
Or Dora the Explorer
Erik Forbes
No wait, that's "We can do it, nothing to it!" - and it's Diego, not Dora. *sigh*
Erik Forbes
+2  A: 

Something like

document.documentElement.innerHTML

This doesn't include the <html> opening and closing tags

David Caunt
I think on IE you can use document.documentElement.outerHTML and don't have to add "<html>" and "</html>".
Alexander Klimetschek
+1  A: 

just call $('html').html() using jquery to get page source.

<html>
<head>
    <title>Just a test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
    $(function() {
      $('#showContent').click(function(event){
       event.preventDefault();
       var pageSource = '<html>' + $('html').html() +'</html>';
       alert(pageSource);
      });
    });
    </script>
</head>
<body>
    <a href="#" id="showContent" >show content</a>
</body>
</html>
Andrea Balducci
That rocks, but is there any way to get at the <html> tags and any other text in the page? ie: DOCTYPE declarations, etc?I'm working on a "passive" markup scraper for w3c validation concerns and need to have access to the full, rendered html of the page being displayed.
kdawg