Javascript Print Local Page Instance

  • 2021-06-29 09:52:01
  • OfStack

The code is simple and practical.Because you need this, you are shameless to pick up your wisdom and collect it for 1 time.


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Untitled Document </title>
<script type="text/javascript">  
  function doPrint() {  
    bdhtml=window.document.body.innerHTML;  
    sprnstr="<!--startprint-->";  
    eprnstr="<!--endprint-->";  
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);  
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));  
    window.document.body.innerHTML=prnhtml; 
    window.print();  
}  
</script>
</head>
 
<body>
<p>1</p>
<p>2</p>
<!--startprint--><!-- Note to add html in star and end These two tags, which were not added before, 1 It just didn't work. Who asked us to serve it? ~-->
<h1> Print title </h1>
<p> print contents ~~</p>
<!--endprint-->
<button type="button" onclick="doPrint()"> Print </button>
<p>1</p>
<p>2</p>
</body>
</html>

Related articles: