Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 213
prabhu r Avatar asked Dec 12 '25 13:12

prabhu r


2 Answers

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>
like image 195
Salman A Avatar answered Dec 14 '25 05:12

Salman A


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.

like image 21
Suresh Ponnukalai Avatar answered Dec 14 '25 03:12

Suresh Ponnukalai