PageSourceSearch

https://www.eix.fr/js/jquery.printElement.js

js eix.fr collected 2026-10-03 02:46:27 UTC 8,404 bytes, 181 lines download raw bytes

1/// <reference path="http://code.jquery.com/jquery-1.4.1-vsdoc.js" />
2/*
3* Print Element Plugin 1.2
4*
5* Copyright (c) 2010 Erik Zaadi
6*
7* Inspired by PrintArea (http://plugins.jquery.com/project/PrintArea) and
8* http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-in-safari-chrome
9*
10*  Home Page : http://projects.erikzaadi/jQueryPlugins/jQuery.printElement 
11*  Issues (bug reporting) : http://github.com/erikzaadi/jQueryPlugins/issues/labels/printElement
12*  jQuery plugin page : http://plugins.jquery.com/project/printElement 
13*  
14*  Thanks to David B (http://github.com/ungenio) and icgJohn (http://www.blogger.com/profile/11881116857076484100)
15*  For their great contributions!
16* 
17* Dual licensed under the MIT and GPL licenses:
18*   http://www.opensource.org/licenses/mit-license.php
19*   http://www.gnu.org/licenses/gpl.html
20*   
21*   Note, Iframe Printing is not supported in Opera and Chrome 3.0, a popup window will be shown instead
22*/
23; (function (window, undefined) {
24    var document = window["document"];
25    var $ = window["jQuery"];
26    $.fn["printElement"] = function (options) {
27        var mainOptions = $.extend({}, $.fn["printElement"]["defaults"], options);
28        //iframe mode is not supported for opera and chrome 3.0 (it prints the entire page).
29        //http://www.google.com/support/forum/p/Webmasters/thread?tid=2cb0f08dce8821c3&hl=en
30        if (mainOptions["printMode"] == 'iframe') {
31            //if ($.browser.opera || (/chrome/.test(navigator.userAgent.toLowerCase())))
32                mainOptions["printMode"] = 'popup';
33        }
34        //Remove previously printed iframe if exists
35        $("[id^='printElement_']").remove();
36
37        return this.each(function () {
38            //Support Metadata Plug-in if available
39            var opts = $.meta ? $.extend({}, mainOptions, $(this).data()) : mainOptions;
40            _printElement($(this), opts);
41        });
42    };
43    $.fn["printElement"]["defaults"] = {
44        "printMode": 'iframe', //Usage : iframe / popup
45        "pageTitle": '', //Print Page Title
46        "overrideElementCSS": null,
47        /* Can be one of the following 3 options:
48        * 1 : boolean (pass true for stripping all css linked)
49        * 2 : array of $.fn.printElement.cssElement (s)
50        * 3 : array of strings with paths to alternate css files (optimized for print)
51        */
52        "printBodyOptions": {
53            "styleToAdd": 'padding:10px;margin:10px;', //style attributes to add to the body of print document
54            "classNameToAdd": '' //css class to add to the body of print document
55        },
56        "leaveOpen": false, // in case of popup, leave the print page open or not
57        "iframeElementOptions": {
58            "styleToAdd": 'border:none;position:absolute;width:0px;height:0px;bottom:0px;left:0px;', //style attributes to add to the iframe element
59            "classNameToAdd": '' //css class to add to the iframe element
60        }
61    };
62    $.fn["printElement"]["cssElement"] = {
63        "href": '',
64        "media": ''
65    };
66    function _printElement(element, opts) {
67        //Create markup to be printed
68        var html = _getMarkup(element, opts);
69
70        var popupOrIframe = null;
71        var documentToWriteTo = null;
72        if (opts["printMode"].toLowerCase() == 'popup') {
73            popupOrIframe = window.open('about:blank', 'printElementWindow', 'width=650,height=440,scrollbars=yes');
74            documentToWriteTo = popupOrIframe.document;
75        }
76        else {
77            //The random ID is to overcome a safari bug http://www.cjboco.com.sharedcopy.com/post.cfm/442dc92cd1c0ca10a5c35210b8166882.html
78            var printElementID = "printElement_" + (Math.round(Math.random() * 99999)).toString();
79            //Native creation of the element is faster..
80            var iframe = document.createElement('IFRAME');
81            $(iframe).attr({
82                style: opts["iframeElementOptions"]["styleToAdd"],
83                id: printElementID,
84                className: opts["iframeElementOptions"]["classNameToAdd"],
85                frameBorder: 0,
86                scrolling: 'no',
87                src: 'about:blank'
88            });
89            document.body.appendChild(iframe);
90            documentToWriteTo = (iframe.contentWindow || iframe.contentDocument);
91            if (documentToWriteTo.document)
92                documentToWriteTo = documentToWriteTo.document;
93            iframe = document.frames ? document.frames[printElementID] : document.getElementById(printElementID);
94            popupOrIframe = iframe.contentWindow || iframe;
95        }
96        focus();
97        documentToWriteTo.open();
98        documentToWriteTo.write(html);
99        documentToWriteTo.close();
100        _callPrint(popupOrIframe);
101    };
102
103    function _callPrint(element) {
104        if (element && element["printPage"])
105            element["printPage"]();
106        else
107            setTimeout(function () {
108                _callPrint(element);
109            }, 50);
110    }
111
112    function _getElementHTMLIncludingFormElements(element) {
113        var $element = $(element);
114        //Radiobuttons and checkboxes
115        $(":checked", $element).each(function () {
116            this.setAttribute('checked', 'checked');
117        });
118        //simple text inputs
119        $("input[type='text']", $element).each(function () {
120            this.setAttribute('value', $(this).val());
121        });
122        $("select", $element).each(function () {
123            var $select = $(this);
124            $("option", $select).each(function () {
125                if ($select.val() == $(this).val())
126                    this.setAttribute('selected', 'selected');
127            });
128        });
129        $("textarea", $element).each(function () {
130            //Thanks http://blog.ekini.net/2009/02/24/jquery-getting-the-latest-textvalue-inside-a-textarea/
131            var value = $(this).attr('value');
132            //fix for issue 7 (http://plugins.jquery.com/node/13503 and http://github.com/erikzaadi/jQueryPlugins/issues#issue/7)
133            //if ($.browser.mozilla && this.firstChild)
134            //    this.firstChild.textContent = value;
135            //else
136                this.innerHTML = value;
137        });
138        //http://dbj.org/dbj/?p=91
139        var elementHtml = $('<div></div>').append($element.clone()).html();
140        return elementHtml;
141    }
142
143    function _getBaseHref() {
144        var port = (window.location.port) ? ':' + window.location.port : '';
145        return window.location.protocol + '//' + window.location.hostname + port + window.location.pathname;
146    }
147
148    function _getMarkup(element, opts) {
149        var $element = $(element);
150        var elementHtml = _getElementHTMLIncludingFormElements(element);
151
152        var html = new Array();
153        html.push('<html><head><title>' + opts["pageTitle"] + '</title>');
154        if (opts["overrideElementCSS"]) {
155            if (opts["overrideElementCSS"].length > 0) {
156                for (var x = 0; x < opts["overrideElementCSS"].length; x++) {
157                    var current = opts["overrideElementCSS"][x];
158                    if (typeof (current) == 'string')
159                        html.push('<link type="text/css" rel="stylesheet" href="' + current + '" >');
160                    else
161                        html.push('<link type="text/css" rel="stylesheet" href="' + current["href"] + '" media="' + current["media"] + '" >');
162                }
163            }
164        }
165        else {
166            $("link", document).filter(function () {
167                return $(this).attr("rel").toLowerCase() == "stylesheet";
168            }).each(function () {
169                html.push('<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" media="' + $(this).attr('media') + '" >');
170            });
171        }
172        //Ensure that relative links work
173        html.push('<base href="' + _getBaseHref() + '" />');
174        html.push('</head><body style="' + opts["printBodyOptions"]["styleToAdd"] + '" class="' + opts["printBodyOptions"]["classNameToAdd"] + '">');
175        html.push('<div class="' + $element.attr('class') + '">' + elementHtml + '</div>');
176        html.push('<script type="text/javascript">function printPage(){focus();print();' + ((opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');
177        html.push('</body></html>');
178
179        return html.join('');
180    };
181})(window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.