Jquery clone() full page source

I am trying to figure out how I can copy the full page source, I have been trying to do it with clone() but not everything is copied and I need to be able to create a full html source code clone from the doctype down to the closing html tag is this possible?

I have tried clone(‘html’) but that is null, if not possible is there any other way using javascript besides going to see the source and copying it?

JavaScript alone only allows you to clone the <body> contents, to clone your entire source you would need to server a server side language such as PHP.

That’s not true, you can certainly get elements from within the <head>.
document.documentElement points to the <html> root element.

One way to get the source is:


document.documentElement.outerHTML

Only tested in Firefox. You might need to do some inspecting.

I am trying this:
$(‘textarea’).text( $(“html”).clone().html() ) ;

  
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
// http://www.w3schools.com/jquery/html_clone.asp
// http://api.jquery.com/contents/
// http://www.sitepoint.com/forums/showthread.php?857518-jquery-clone%28%29-full-page-source

$(document).ready(function(){

  $("button").click(function(){


/*
var n = $("html").contents(); 
alert(n.length); // 3
for(var i =0; i< n.length; i++) {
alert( $(n[i]).html()); 
//alert( $(n[i]).parent().html()); 
}
*/

//alert(  $("html").clone().html() );
//$('div').text( $("html").clone().html() ) ;

//alert(  $("html").clone().text() );
// $('div').text( $("html").clone().text() ) ;

//$('textarea').text( $("html").clone().text() ) ;

$('textarea').text( $("html").clone().html() ) ;

  });
});

</script>
</head>
<body>
<button>Clicm me</button>
<div></div>
<textarea rows="10" cols="50"></textarea>
</body>
</html>