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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With