javascript - Print content of jquery dialog to a printer - Stack Overflow

I have the following jQueryUI dialog.How can I print the content of the dialog?The content could be

I have the following jQueryUI dialog. How can I print the content of the dialog? The content could be any HTML such as a table, image, etc. There were several earlier answers to this question,however, they appear to be out of date.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Dialog</title>
        <script src=".js" type="text/javascript"></script> 
        <link rel="stylesheet" href=".10.1/themes/base/jquery-ui.css" />
        <script src=".10.1/jquery-ui.js"></script>

        <script>
            $(function() {
                $("#openDialog").click(function(){$("#myDialog").dialog('open')});
                $( "#myDialog" ).dialog({
                    modal: true,
                    autoOpen    : false,
                    buttons: {Ok: function() {alert('print');}}
                });
            });
        </script>
    </head>
    <body>
        <button id="openDialog">Click</button>
        <div id="myDialog" title="My Dialog">
            <p>Print this text!</p>
            <img alt="And print this image" src="myImg.png">
        </div>
    </body>
</html>

I have the following jQueryUI dialog. How can I print the content of the dialog? The content could be any HTML such as a table, image, etc. There were several earlier answers to this question,however, they appear to be out of date.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Dialog</title>
        <script src="http://code.jquery./jquery-latest.js" type="text/javascript"></script> 
        <link rel="stylesheet" href="http://code.jquery./ui/1.10.1/themes/base/jquery-ui.css" />
        <script src="http://code.jquery./ui/1.10.1/jquery-ui.js"></script>

        <script>
            $(function() {
                $("#openDialog").click(function(){$("#myDialog").dialog('open')});
                $( "#myDialog" ).dialog({
                    modal: true,
                    autoOpen    : false,
                    buttons: {Ok: function() {alert('print');}}
                });
            });
        </script>
    </head>
    <body>
        <button id="openDialog">Click</button>
        <div id="myDialog" title="My Dialog">
            <p>Print this text!</p>
            <img alt="And print this image" src="myImg.png">
        </div>
    </body>
</html>
Share Improve this question edited Jan 8, 2014 at 17:22 user1032531 asked Jan 8, 2014 at 16:56 user1032531user1032531 26.4k75 gold badges245 silver badges416 bronze badges 1
  • Can you explain exactly what do you want? Do you want to print contents from the modal into a separate div# or do you want to print your modal p and img (or whatever) when you click Ok button, into an alert window as I see in the code? – Robert W. Hunter Commented Jan 8, 2014 at 17:13
Add a ment  | 

3 Answers 3

Reset to default 1

I developed my own plugin with code is below, and a live example is located at http://jsfiddle/fyu4P/embedded/result/.

On FF 26.0, it works, however, after printing a couple of times, FF asks the user if popups should be disabled which I wish not to happen. Also, it doesn't work with older IE, and likely other browsers. Don't worry, when printing, you still need to click the operating system print dialog, so you won't waste any paper! I've asked https://codereview.stackexchange./questions/39235/critique-of-jquery-plugin-which-will-print-to-a-printer-an-element-or-a-jqueryui for any remendations.

Actual Plugin:

/*
* jQuery printIt
* Print's the selected elements to the printer
* Copyright Michael Reed, 2014
* Dual licensed under the MIT and GPL licenses.
*/
(function($){
    var defaults = {
        elems           :null, //Element to print HTML
        copy_css        :false,//Copy CSS from original element
        external_css    :null  //New external css file to apply
    };

    var methods = {
        init : function (options) {
            var settings = $.extend({}, defaults, options)
            elems=$(settings.elems);
            return this.each(function () {
                $(this).click(function(e) {
                    var iframe   = document.createElement('iframe');
                    document.body.appendChild(iframe);
                    $(iframe).load(function(){
                        elems.each(function(){
                            iframe.contentWindow.document.body.appendChild(this.cloneNode(true));
                        });
                        if(settings.copy_css) {
                            var arrStyleSheets = document.getElementsByTagName("link");
                            for (var i = 0; i < arrStyleSheets.length; i++){
                                iframe.contentWindow.document.head.appendChild(arrStyleSheets[i].cloneNode(true));
                            }
                            var arrStyle = document.getElementsByTagName("style");
                            for (var i = 0; i < arrStyle.length; i++){    
                                iframe.contentWindow.document.head.appendChild(arrStyle[i].cloneNode(true));
                            }
                        }
                        if(settings.external_css) {
                            var style  = document.createElement("link")
                            style.rel  = 'stylesheet';
                            style.type = 'text/css';
                            style.href = settings.external_css;
                            iframe.contentWindow.document.head.appendChild(style);
                        }
                        var script   = document.createElement('script');
                        script.type  = 'text/javascript';
                        script.text  = 'window.print();';
                        iframe.contentWindow.document.head.appendChild(script);
                        $(iframe).hide();
                    });
                });
            });
        },
        destroy : function () {
            //Anything else I should do here?
            delete settings;
            return this.each(function () {});
        }
    };

    $.fn.printIt = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.printIt');
        }    
    };
    }(jQuery)
);

To configure:

$(function () {
    $("#openDialog").click(function () {
        $("#myDialog").dialog('open')
    });
    $("#myDialog").dialog({
        modal: true,
        autoOpen: false
    });
    $('#printIt').printIt({
        elems: $("#myDialog"),
        copy_css: true,
        external_css: 'test2.css'
    });
});

This will add a printable area, and puts modal html into it.

$(function () {
    $("#openDialog").click(function () {
        $("#myDialog").dialog('open')
    });
    $("#myDialog").dialog({
        modal: true,
        autoOpen: false,
        buttons: {
            Ok: function (e) {
                $('#divToPrint').html($(this)[0].innerHTML).printArea();
            }
        }
    });
});

You need to create a new <div id="divToPrint"></div> if you want to not display the new div, just use style="display: none;"

Then when you press CTRL+P it will print what you created...

Try like below.....and call your div id. You should be good to go.

buttons: { 
            "Print": function() { 
                $("#dialog").printArea(); 
            },
            "Close": function() { 
               $(this).dialog("close");
            }
        }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744760561a4592150.html

相关推荐

  • javascript - Print content of jquery dialog to a printer - Stack Overflow

    I have the following jQueryUI dialog.How can I print the content of the dialog?The content could be

    18小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信