Problems with the Print function

Source: Internet
Author: User

Demand

There is a "print" function in the pop-up window.

At the beginning of the network to find a solution:

Http://www.cnblogs.com/huichao1314/p/5545992.html refer to the blog below.

Main code:

Js

varoper = 1;//Initial value    functionPreview (oper) {if(Oper < 10) {bdhtml= Window.document.body.innerHTML;//gets the HTML code for the current pageSprnstr = "<!--startprint" + oper + "-";//Set the print start area            /*alert (SPRNSTR)*/Eprnstr= "<!--endprint" + oper + "-";//Set the print end areaprnhtml = bdhtml.substring (Bdhtml.indexof (SPRNSTR) + 18);//To take HTML back from the start codeprnhtml = prnhtml.substring (0, Prnhtml.indexof (eprnstr));//fetching HTML forward from the end codeWindow.document.body.innerHTML =prnhtml;            Window.print (); Window.document.body.innerHTML=bdhtml; /*window.location.reload ();*/            //Refresh Page}Else{window.print (); }    }

Html

And then in the code that you need to print, use the<!--Startprint1 -And<!--Endprint1 -surrounded, as follows:---------------------------------------------<!--Startprint1 -<!--Print content Start -<DivID=sty>    ...</Div><!--end of printed content -<!--Endprint1 ----------------------------------------------finally add a Print button<inputtype=buttonname= ' Button_export 'title= ' Print 1 'onclick=preview (1)value= print 1>

This can be achieved by printing, but there is a problem:

With the widow of the printing, the point "Cancel", will cause the first pop-up window interface "Cancel", invalid. That is, figure 1 cannot be closed.

In order to solve this problem, change the solution:

JS call iframe to implement the method of printing page content

This scenario is

1) This program can be implemented to select the area of the page to print, the IFRAME way to print;
2) The difference from the original print (Method 1) is that the content of the current access page can be completely retained after canceling the printed page . (resolves the problem)

HTML code:

//Print Area: < Div id= "sty" > ...... </ Div > //Call to print <  onclick= "Print (' sty ');" > print </button>

Js

    functionPrint (id_str) {varEl =document.getElementById (ID_STR); variframe = document.createelement (' iframe '); varDoc =NULL;        Document.body.appendChild (IFRAME); Doc=iframe.contentWindow.document; Doc.write (' <div> ' + el.innerhtml + ' </div> ');        Doc.close ();        Iframe.contentWindow.focus ();        Iframe.contentWindow.print (); if(Navigator.userAgent.indexOf ("MSIE") > 0) {document.body.removeChild (IFRAME); }    }

Test found that after entering the print page canceled, it will not affect the first pop-up window

See below Https://www.jb51.net/article/47622.htm

Problems with the Print function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.