printThis

jQuery print element function

by winnythepooh1

HTML

<div>
<button id="printKitten">Print</button>
<p>Lorem ipsizzle hizzle sit i saw beyonces tizzles and my pizzle went crizzle, consectetuer adipiscing fo shizzle mah nizzle fo rizzle, mah home g-dizzle. Sizzle dizzle velit, boom shackalack volutpizzle, suscipizzle i'm in the shizzle, shut the shizzle up vizzle, fo shizzle. Pellentesque izzle pizzle. Sed erizzle. Shizzle my nizzle crocodizzle izzle owned bow wow wow turpizzle tempizzle tempor. Maurizzle funky fresh nibh et turpizzle. Gangsta izzle dizzle. Pellentesque sizzle rhoncizzle yo mamma. In rizzle habitasse platea stuff. Fo shizzle my nizzle dapibus. Yippiyo check out this owned, pretium dope, dang ac, eleifend dizzle, shut the shizzle up. Check out this suscipizzle. Integizzle fo shizzle owned you son of a bizzle shizznit.</p>
<img id="print" src="http://placekitten.com/g/200/300" />
<p>Curabitur et dizzle quis nisi ghetto mollizzle. Suspendisse we gonna chung. Morbi odio. Go to hizzle neque. i'm in the shizzle. Check out this maurizzle cool, interdum a, owned sit amizzle, phat izzle, pede. Pellentesque my shizz. Vestibulum ass pot, volutpizzle izzle, check it out sizzle, fo shizzle owned, velizzle. Crizzle izzle ipsizzle. Da bomb volutpizzle felizzle vizzle orci. Pizzle stuff justo owned purizzle shiznit ornare. Doggy venenatizzle justo izzle lacus. Black nizzle. Suspendisse venenatis placerizzle pizzle. Curabitur sure ante. Fo shizzle pharetra, gangster eu check it out hendrerizzle, break it down felizzle i'm in the shizzle sizzle, izzle we gonna chung magna yo luctus pede. Pot a nisl. Dang aptent taciti crazy izzle litora torquent mofo conubia shizzle my nizzle crocodizzle, pizzle inceptizzle hymenizzle. Bow wow wow interdizzle, its fo rizzle nec dang nonummy, pimpin' orci black things, my shizz sempizzle risus arcu funky fresh yo mamma.</p>
</div>

CSS

p {padding: 10px;}
img {float: left; padding: 10px;}

JavaScript

$("#printKitten").click(function() {
    $("#print").printThis({
  //      debug: true
    });
});


// -----------------------------------------------------------------------
// printThis v1.1
// Printing plug-in for jQuery
//
// Resources (based on) :
//              jPrintArea: http://plugins.jquery.com/project/jPrintArea
//              jqPrint: https://github.com/permanenttourist/jquery.jqprint
//              Ben Nadal: http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
//
// Dual licensed under the MIT and GPL licenses:
//              http://www.opensource.org/licenses/mit-license.php
//              http://www.gnu.org/licenses/gpl.html
//
// (c) Jason Day 2012
//
// Usage:
//
// $("#mySelector").printThis({
//      debug: false, //show the iframe for debugging
//      importCSS: true, // import page CSS
//      printContainer: true, // grab outer container as well as the contents of the selector
//      loadCSS: "path/to/my.css" //path to additional css file
//  });
//
// Notes:
//  - the loadCSS option does not need @media print
//------------------------------------------------------------------------
 
(function($) {
    var opt;
 
    $.fn.printThis = function (options) {
        opt = $.extend({}, $.fn.printThis.defaults, options);
 
        var $element = (this instanceof jQuery) ? this : $(this);
     
    // if Opera, open a new tab
        if ($.browser.opera)
        {
            var tab = window.open("","Print Preview");
            tab.document.open();
 
            var doc = tab.document;
        }
    // add dynamic iframe to DOM
        else
        {
        var strFrameName = ("printThis-" + (new Date()).getTime());
         
            var $iframe = $("<iframe id='" + strFrameName +"' src='about:blank'/>");
 
            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
 
            $iframe.appendTo("body");
         
            var...