Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Printing just a paragraph?

Hi,
I'm presenting my story and want the final page to print out - but just one paragraph! e.g. not any links to other passages.

I'm using harlowe and have had no problem installing a link to print the page, but haven't found a code that allows me to print just a section.

Anyone got any suggestions?

Thanks

Comments

  • warning: The following solution may not work on all web-browser, nor on all devices.

    You did not give an example of the method you are using to print the current page, so I am going to assume you are using the Javascript window.print() function, which can be used to print the current document.

    How the window.print() works is determined by either the web-browser or the operating system you use, but it is meant to print out all of the current document. You can get around this by temporarily placing the content you want printed into an iframe element.

    The following solution consists of two parts:

    1. The Javascript which temporarily copies a marked section of text into a hidden iframe element which is then passed to the web-browser's Print system. The following code needs to be placed within your Story Javascript area, it adds a printSection function to your story:
    if (!window.printSection){
    	window.printSection = function(section){
    		var element = document.getElementById(section);
    		if (!element) {
    			throw new Error('printSection: the section "' + section + '" does not exist');
    		}
    		if (!window.frames["print_frame"]) {
    			throw new Error('printSection: the "print_frame" iframe does not exist');
    		}
    		window.frames["print_frame"].document.body.innerHTML = element.innerHTML;
    		window.frames["print_frame"].window.focus();
        window.frames["print_frame"].window.print();
    	};
    }
    

    2. The Passage code/markup used to indicate what section should be printed and the hidden iframe to use to print the section:
    The example uses a section element with an id of results to mark the text to be included in the printout, it uses a hyperlink to call the printSection function and it contains a hidden iframe element which will be used by the printSection function:
    This text will not be included in the printout.
    
    <section id="results">
    This text will be included in the printout.
    </section>
    <a href="javascript:window.printSection('results');">Print the 'results' section.</a>
    
    This text will not be included in the printout.
    
    <iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>
    
Sign In or Register to comment.