Load dynamically an external CSS file (with a media="print") like on this page if the textarea is empty. The print function (call by a button or link) will simply have to contain this lines
Code:
if (!(document.getElementById('myTextarea').value.replace(/\s/g,'')) {
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("media", "print");
fileref.setAttribute("href", "filename.css"); // With the right filename
if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}
The replace function remove blanks, tabs, carriage returns...
EDIT : It could be necessary to call the window.print() with a delay (a setTimeout) to assure the load of the css file or to attach an onload event on the link.
<script type="text/javascript">
function specialPrint() {
if (!(document.getElementById('printarea').value.replace(/\s/g,'')) {
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("media", "print");
fileref.setAttribute("href", "pf-style.css"); // With the right filename
if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
</script>
Wouldn't it be possible to do something like this?
A bracket is missing (line 3) to close (after the replace function) condition and negation !
Document.write is to use to build a new document (when the document is open and not closed). Then it would clear and remove the whole document after a jQuery document.ready !
Bookmarks