I have a simple button on my ASP.NET MVC view:
<input type="button" onclick="print()" value="Print" />
Which on click calls this JavaScript function:
<script type="text/javascript" language="javascript">
function print() {
window.print();
}
</script>
However when I click the "Print" button I get the following error:
96:252 Uncaught RangeError: Maximum call stack size exceeded
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
at print (96:252)
I'm not exactly sure what those numbers are refering to, but I am at least sure that they are not lines in my code. I don't have a line 252 and on 96 there is just a <dt> tag.
I have also tried this:
<input type="button" onclick="window.print()" value="Print" />
and this:
<input type="button" onclick="javascript:window.print()" value="Print" />
and this:
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="javascript:print();" />
But got the same error (the third try I can't even get the asp:Button to appear). Now I know the error means that in the code I am calling a function which in turn calls another function and so forth, until it hits the call stack limit. However, I'm not sure why my code is causing this to happen.
If I try to not use the button at all and simply add:
<script>
window.print();
</script>
At the bottom of the view then everything works, but this is not the desired behavior. It should only print if the user clicks the button.
Why is my code causing this error? What can I change to fix it?
<input type="button" onclick="window.print()" value="Print" />