views:

246

answers:

3

Is there a way to acces the page HTML source code using javascript? I know that i can use the document.body.innerHTML but it contains only the code inside the body. I want to get all the page source code including head and body tags with their content, and, if it's possible, also the html tag and the doctype. Is it possible?

+6  A: 

Use document.documentElement.outerHTML/innerHTML

Eldar Djafarov
i don't know why in Firefox the document.documentElement object doesn't have the outerHTML property, but with the innerHTML i can get almost everything except the doctype so thank you!
mck89
@mck89: no browser but IE will have `outerHTML`.
Crescent Fresh
Be aware that the source you get with Firefox/most browsers is the "true" source you served up. In IE you will get the "live" HTML of the page including any changes the user has made to forms, any new DOM content etc. In IE it will also be the mixed case invalid tag soup that IE provides when requesting the .innerHTML of elements.
scunliffe
+3  A: 

One way to do this would be to re-request the page using XMLHttpRequest, then you'll get the entire page verbatim from the web server.

Paul Dixon
A: 

For IE you can also use: document.all[0].outerHTML

DmitryK