9

I have HTML and I need to get page source of this html.

document.documentElement.outerHTML

or

$.ajax({
async: true,
type: 'GET',
cache: false,
url: window.location.href,
success: function(data) {
   alert(data);
}
});

is working, but they display originally source. If I change html (by jQuery, for example), they don't read my changes.

Is it possible read CURRENT source of page?

indapublic
  • 2,208
  • 9
  • 38
  • 49

4 Answers4

11

Tried it on chrome and it works. Use

document.documentElement.innerHTML

Funnily enough your code also worked

document.documentElement.outerHTML

Check out the html printed to the console in this fiddle. It actually contains the changes made by jQuery.

Bruno
  • 5,772
  • 1
  • 26
  • 43
  • Note that this will include that have been made to the DOM. For example, some plugins inject HTML, and if you resize a – Jordan Eldredge Feb 14 '15 at 02:05
5

Using jQuery you can do this by:

$( 'body' ).html();

For example.

Or convert to String:

$( 'body' ).html().toString();
antyrat
  • 27,479
  • 9
  • 75
  • 76
1

You just need to grab the element and use the html method:

$('.selector').html();
Tomer
  • 17,787
  • 15
  • 78
  • 137
-1

Simple solution, source of a document usually embedded in html tag so use $('html').html(), you will get everything between html tags.

Legionar
  • 7,472
  • 2
  • 41
  • 70