Assuming that by 'print' you don't actually mean to transfer it to a paper copy, you can add some script like:
window.addEventListener('load', function() {
var content = document.documentElement.innerHTML,
pre = document.createElement('pre'),
body = document.body;
pre.innerText = content;
body.insertBefore(pre, body.firstChild);
});
What this does, step by step is:
window.addEventListener('load', function()
> Wait for the page to be fully loaded and then execute the function
content = document.documentElement.innerHTML
> store the actual page source in the content
variable (document.documentElement
refers to the 'root'-node, usually <html>
in html documents
pre = document.createElement('pre')
> create a new <pre>
-element
body = document.body
> create a reference to the <body>
element
pre.innerText = content
> assign the HTML-structure we've stored earlier as text to the <pre>
-element
body.insertBefore(pre, body.firstChild)
> put the <pre>
-element (now with contents) before any other element in the body (usually on top of the page).
This leaves you with the entire source (as it was before creating the <pre>
-element containing the source) on top of you page.
Edit: Added <iframe>
workflow
It was not clear to me you actually wanted to target an <iframe>
, so here's how to do that (using a naive approach, more on that further on):
window.addEventListener('load', function() {
var iframeList = document.getElementsByTagName('iframe'),
body = document.body,
content, pre, i;
for (i = 0; i < iframeList.length; ++i) {
content = iframeList[i].documentElement.innerHTML;
pre = document.createElement('pre');
pre.innerText = content;
body.insertBefore(pre, body.firstChild);
}
});
why is this approach naive?
There is a thing called Same-Origin-Policy in javascript, which prevents you from accessing <iframe>
-content which if the contents do not originate from the same domain as the page containing the <iframe>
.
There are several ways to take this into consideration, you could wrap the inside of the for
-loop in try/catch
-blocks, though I prefer to use a more subtle approach by not even considering <iframes>
which do not match the Same-Origin-Policy.
In order to do this, you can swap the getElementsByTagName
method with the querySelectorAll
method (please note the compatibility table at the bottom of that page, see if it matches your requirements).
The querySelectorAll
accepts a valid CSS selector and will return a NodeList
containing all matching elements.
A simple selector to use would be
'iframe[src]:not([src^="//"]):not(src^="http")'
which selects all iframe with a src
attribute which does not start with either //
or http
Disclaimer: I never use a <base>
-tag (which changes all relative paths within the HTML) or refer to the current website using a path containing the domain, so the example CSS-selector does not consider these aberrations.
Can you use :not()
IE9 or better
Can you use document.querySelector(All)
IE8 or better (in order to use with :not()
, IE9 or better)
hover/click the boxes above to show the spoiler