Using JQuery to open a popup window and print

前端 未结 3 937
挽巷
挽巷 2020-12-09 10:42

A while back I created a lightbox plugin using jQuery that would load a url specified in a link into a lightbox. The code is really simple:

$(\'.readmore\')         


        
3条回答
  •  时光说笑
    2020-12-09 11:11

    Got it! I found an idea here

    http://www.mail-archive.com/discuss@jquery.com/msg18410.html

    In this example, they loaded a blank popup window into an object, cloned the contents of the element to be displayed, and appended it to the body of the object. Since I already knew what the contents of view-details (or any page I load in the lightbox), I just had to clone that content instead and load it into an object. Then, I just needed to print that object. The final outcome looks like this:

    $('.printBtn').bind('click',function() {
        var thePopup = window.open( '', "Customer Listing", "menubar=0,location=0,height=700,width=700" );
        $('#popup-content').clone().appendTo( thePopup.document.body );
        thePopup.print();
    });
    

    I had one small drawback in that the style sheet I was using in view-details.php was using a relative link. I had to change it to an absolute link. The reason being that the window didn't have a URL associated with it, so it had no relative position to draw on.

    Works in Firefox. I need to test it in some other major browsers too.

    I don't know how well this solution works when you're dealing with images, videos, or other process intensive solutions. Although, it works pretty well in my case, since I'm just loading tables and text values.

    Thanks for the input! You gave me some ideas of how to get around this.

提交回复
热议问题