prabhu r
prabhu r

Reputation: 181

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>

Upvotes: 3

Views: 14069

Answers (3)

Salman Arshad
Salman Arshad

Reputation: 272336

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>

Upvotes: 10

anshuraj
anshuraj

Reputation: 342

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>

Upvotes: 0

Suresh Ponnukalai
Suresh Ponnukalai

Reputation: 13998

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.

Upvotes: 1

Related Questions