Raymond Morphy
Raymond Morphy

Reputation: 2526

Window.print() fails to print

For printing my Aspx web page I use following code but in IE I encounter "Stack over flow at line:0" error message and in fire fox any thing doesn't happen. what's wrong?

<head>
 <script language="javascript" type="text/javascript">
        function print() {
            window.print();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
<div class="toolbar" style="width:400px">
            <ul>
<li>
                <img alt="" src="../../../CssImages/printer_128.png" id="ImgPrint" width="20px" style="cursor:pointer" onclick="print()"/>
                </li>
     </ul>

Upvotes: 2

Views: 526

Answers (1)

pimvdb
pimvdb

Reputation: 154818

Your function:

function print() {
    window.print(); // <-- refers to this custom function
}

will put print into the global object as window.print. So in fact you're calling the function itself, which calls itself, etc. This will continue forever which causes an overflow.

Because window.print is already defined natively, why not eliminate the custom function? If you remove function print() {...} it should work fine, because it will then call the 'real' window.print (as print is just a shortcut to window.print), which actually does the printing.

Upvotes: 7

Related Questions