简体   繁体   中英

window.print() not working in Chrome

In the below code, the window.print() function not working in Chrome.what to do?

 <div class="row"> <div class="col-sm-3" id="itemin"> <input type="text" placeholder="Enter Item Name" size="20"> </div> <div class="col-sm-2" id="qtyin"> <input type="num" placeholder="quantity" size="20" id="qty"> </div> <div class="col-sm-3" id="rsin"> <input type="text" placeholder="prize" size="20" id="rs"> <button onclick="mul()" id="add">add </button> </div> <div class="col-sm-3" id="amtin"> <input type="text" size="20" id="amt"> <input type="text" size="7" id="total"> <button onclick="print()" id="print"> print </div> </div> <script> function print() { window.print(); } </script> 

Congratulations, you caused a StackOverflow!

This code:

function print() {
    window.print();
}

is same as writing:

window.print = function {
    window.print();
}

Basically you overwrote the native window.print function with your own function that calls window.print recursively. The solution is simple... remove your function and simply:

<button onclick="window.print()" id="print">print</button>

Or this:

<button onclick="myPrintFunction()" id="print">print</button>
<script>
    function myPrintFunction() {
        // do something maybe
        window.print();
    }
</script>

It is because of it went on infinite loop. The code onclick="print()" will automatically call the window.print() .

Either you change the function name or remove the print function. It will work.

Just call window.print() in the onclick of button. No need to define a function for it.

<body>
<div class="row">
 <div class="col-sm-3" id="itemin"> <input type="text" placeholder="Enter Item Name" size="20"> </div>
 <div class="col-sm-2" id="qtyin">
   <input type="num" placeholder="quantity" size="20" id="qty"></div>
 <div class="col-sm-3" id="rsin"> <input type="text" placeholder="prize" size="20" id="rs"> <button onclick="mul()" id="add">Add </button> </div>
 <div class="col-sm-3" id="amtin">
   <input type="text"  size="20" id="amt">
   <input type="text" size="7" id="total">
   <button onclick="window.print()" id="print">Print</button>
    </div>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM