Possible to put HTML page source into a variable?

L

laredotornado

Hi,

Is there a JS way to store the current page's HTML source in a
variable? Thanks, - Dave
 
G

Gregor Kofler

Am 2011-05-31 22:52, laredotornado meinte:
Hi,

Is there a JS way to store the current page's HTML source in a
variable? Thanks, - Dave

var src = document.documentElement.innerHTML;

(Haven't checked for any cross-browser issues.)

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Am 2011-05-31 22:52, laredotornado meinte:

var src = document.documentElement.innerHTML;

(Haven't checked for any cross-browser issues.)

While being very compatible for historical reasons, that would not store the
tags of the root element, the DOCTYPE declaration and anything else before
or – $DEITY forbid! – after the root element. MSHTML has .outerHTML, of
course, but ISTM that you have to serialize the missing parts yourself as
unfortunately there is no document.innerHTML.

A non-equivalent alternative is XHR:

/* or ActiveXObject(…) where necessary, like with file:// in MSHTML */
var x = new XMLHttpRequest();

x.open("GET", document.URL, false);
x.send(null);

var src = x.responseText;


PointedEars
 
D

Dr J R Stockton

Wed said:
Am 2011-05-31 22:52, laredotornado meinte:

var src = document.documentElement.innerHTML;

(Haven't checked for any cross-browser issues.)

In Firefox 3.6.17, not a full exact copy. Seems to regenerate from a
tokenised version or from the DOM tree. DOCTYPE not represented. And
"</li>" present, which the original did not have.

In some, but not all browsers, as I recall, the page can be loaded into
a iframe, in which it can be accessed verbatim. One browser will not
read a file from the local disc even though the current page is in the
same directory; another could, but would not read the same page as it
was showing.

document.body.innerHTML may suffice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top