最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript: Get current page CURRENT source - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Dec 17, 2012 at 12:03 indapublicindapublic 2,31810 gold badges38 silver badges50 bronze badges 4
  • 2 You dont want the Source, but the DOM? – EricG Commented Dec 17, 2012 at 12:08
  • How about document.getElementsByTagName("HTML")[0].outerHTML? – EricG Commented Dec 17, 2012 at 12:25
  • Why do you think that document.documentElement.outerHTML would not work? – Bergi Commented Dec 17, 2012 at 13:24
  • Possible duplicate of How to get the entire document HTML as a string? – wesinat0r Commented Oct 14, 2019 at 1:32
Add a comment  | 

4 Answers 4

Reset to default 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.

Using jQuery you can do this by:

$( 'body' ).html();

For example.

Or convert to String:

$( 'body' ).html().toString();

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

$('.selector').html();

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

发布评论

评论列表(0)

  1. 暂无评论