Javascript Page Print Functionality

Robert Granlund
Robert Granlund used Ask the Experts™
on
Javascript.  Is there a way to add print page functionality to this script used here?  It is an ePub and I have the source code and have added a button on my local machine but it wants to print the entire thing.  Any suggestions?
http://www.milestonedocuments(DOT)com/assets/reader/?epub=epub_content%2FWestern_Civ_2_Standard_Edition.epub&goto=epubcfi(/6/24!/4%5BWestern_Civ_2_Standard_Edition-10%5D/2/84/1:133)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
URL provided fails.

This URL Fix seems to work.

Unsure why you're trying to do this via Javascript.

Every OS provides it's own approach to printing.

Usually CNTL+P or something similar.

Unsure what you might add by providing some sort of custom printing facility.

Tip: Describe why OS printing facility won't work for you + what value you envision adding using some sort of Javascript printing facility.

Author

Commented:
@David Favor I don't like to place the url in posts because it will get indexed. So I always use (DOT) is there a better way to do that?
If you go to that ePub and you click ctrl+P you will see it does not want to print or wants to print the entire thing.  There is an iFrame and I would like to print what is in the content of a specific div.  The following does not work:

<script>
function printPage(divName){
			var printContents = document.getElementById(divName).innerHTML;
			var originalContents = document.body.innerHTML;
			document.body.innerHTML = printContents;
			window.print();
			document.body.innerHTML = originalContents;
		}
<button id="printButt" tabindex="1" type="button" class="btn icon-print" onclick="printPage('epubContentIframe')"/>Print Page</button>

Open in new window

David FavorFractional CTO
Distinguished Expert 2018

Commented:
You said, "I don't like to place the url in posts because it will get indexed. So I always use (DOT) is there a better way to do that?"

First question to answer is if you own the https://milestonedocuments.com site.

If you do own this site, then your starting point will be searching for something like - how to print part of a page using javascript - which turns up 650M results, so likely something useful to find in these search results. I'd probably start with GitHub projects.

Scanning a few results, this doesn't appear overly hard to implement. Will just take someone sitting down + writing code, so will require a good bit of time to code + test + verify in all browsers.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Will it be the same for printing part of an iFrame?  Is there anything special I need to do when it comes to targeting an ID within an iFrame?
leakim971Multitechnician
Top Expert 2014

Commented:
this work fine on Chrome and FF :
setTimeout(function() { iframe = document.querySelector("iframe").contentWindow; iframe.focus(); iframe.print(); }, 500);

Readium.pdf

Author

Commented:
@leakim971 Can you elaborate a little more on this solution?  I'm not sure I know how to implement this. How would I attach that to an onClick event?
leakim971Multitechnician
Top Expert 2014

Commented:
yes or any other event able to fire the code
leakim971Multitechnician
Top Expert 2014
Commented:
<button onclick="runIt();">print</button>

Author

Commented:
function runit() {
setTimeout(function() { iframe = document.querySelector("iframe").contentWindow; iframe.focus(); iframe.print(); }, 500);
}

Is that correct?

Also, in that function can I target a specific ID in the iFrame?  If so, how?
Multitechnician
Top Expert 2014
Commented:
Is that correct?
yes

can I target a specific ID in the iFrame
replace :
document.querySelector("iframe")
by :
document.getElementById("your iframe id here")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial